From 8d8cfa1c4cafd2462ece5244ab39272e8db600b9 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 27 Nov 2025 12:19:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PdfReader.razor.cs | 21 +++++++++- .../PdfReader.razor.js | 41 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs b/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs index ad3cfc36..9283384e 100644 --- a/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs +++ b/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs @@ -110,11 +110,17 @@ public partial class PdfReader public string? MoreButtonIcon { get; set; } /// - /// 点击下载按钮回调方法 默认 null 使用组件内置下载功能 + /// 点击下载按钮回调方法 默认 null /// [Parameter] public Func? OnDownloadAsync { get; set; } + /// + /// 正在打印回调方法 默认 null + /// + [Parameter] + public Func? OnPrintingAsync { get; set; } + private string? ClassString => CssBuilder.Default("bb-pdf-reader") .AddClassFromAttributes(AdditionalAttributes) .Build(); @@ -347,4 +353,17 @@ public async Task PageChanged(uint pageIndex) await OnPageChangedAsync(pageIndex); } } + + /// + /// 正在打印回调方法 + /// + /// + [JSInvokable] + public async Task Printing() + { + if (OnPrintingAsync != null) + { + await OnPrintingAsync(); + } + } } diff --git a/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js b/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js index e7b77a5f..ef6a768c 100644 --- a/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js +++ b/src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js @@ -127,7 +127,7 @@ const addEventListener = (el, pdfViewer, eventBus, invoke, options) => { } if (options.triggerPagesInit === true) { - await invoke.invokeMethodAsync("pagesInit", numPages); + await invoke.invokeMethodAsync("PagesInit", numPages); } }); @@ -139,6 +139,12 @@ const addEventListener = (el, pdfViewer, eventBus, invoke, options) => { if (options.triggerPagesLoaded === true) { await invoke.invokeMethodAsync("PagesLoaded", e.pagesCount); } + + const controls = el.querySelector(".bb-view-controls"); + EventHandler.on(controls, "click", ".bb-view-print", e => { + printPdf(options.url); + await invoke.invokeMethodAsync("Printing"); + }); }) eventBus.on("pagechanging", async evt => { @@ -276,6 +282,31 @@ const makeThumb = async page => { return canvas; } +const printPdf = url => { + let iframe = document.querySelector(".bb-view-print-iframe"); + if (iframe) { + iframe.remove(); + } + + iframe = document.createElement("iframe"); + iframe.classList = "bb-view-print-iframe"; + iframe.style.position = "fixed"; + iframe.style.right = "100%"; + iframe.style.bottom = "100%"; + iframe.src = url; + + iframe.onload = () => { + iframe.contentWindow.addEventListener('afterprint', function () { + document.body.removeChild(iframe); + }); + + iframe.contentWindow.focus(); + iframe.contentWindow.print(); + }; + + document.body.appendChild(iframe); +} + export function dispose(id) { Data.remove(id); @@ -304,5 +335,13 @@ export function dispose(id) { if (thumbnailsContainer) { EventHandler.off(thumbnailsContainer, "click"); } + + const controls = el.querySelector(".bb-view-controls"); + EventHandler.off(controls, "click"); + + const iframe = document.querySelector('.bb-view-print-iframe'); + if (iframe) { + iframe.remove(); + } } } From 6a4fa90482661577a1c979fa9ae038ce1cc44488 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 27 Nov 2025 12:21:25 +0800 Subject: [PATCH 2/2] feat(PdfReader): add OnPrintingAsync parameter --- .../BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj b/src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj index 155c9517..b8f6709e 100644 --- a/src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj +++ b/src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj @@ -1,7 +1,7 @@  - 10.0.1-beta07 + 10.0.1-beta08