亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 xaml 頁面轉換為 pdf 文件并在 UWP 應用程序內的 pdf 查看器中顯示?

如何將 xaml 頁面轉換為 pdf 文件并在 UWP 應用程序內的 pdf 查看器中顯示?

C#
喵喵時光機 2022-11-21 22:06:27
我正在嘗試使用 Syncfusion PDF 查看器,但為了使用它,我最終必須將 xaml 頁面保存為 pdf 文件。我怎么做?我點擊了此鏈接,但它沒有給我想要的東西。XAML 到 PDF 轉換我正在嘗試使用下面的代碼來保存 xaml 頁面 [ new GenericManifestPDF(_manifestPDFDataModel); ] 從我的視圖模型類中得到這個錯誤:' Value does not fall within the expected range ' in this line await renderTargetBitmap.RenderAsync(new GenericManifestPDF(_manifestPDFDataModel));   PdfDocument document = new PdfDocument();   //Add a new page   PdfPage page = document.Pages.Add();   //Initialize render to bitmap   var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;   var renderTargetBitmap = new RenderTargetBitmap();   //Create a Bitmap from XAML page   await renderTargetBitmap.RenderAsync(new GenericManifestPDF(_manifestPDFDataModel));   var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();   //Save the XAML in bitmap image   using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())   {   var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);   encoder.SetPixelData(   BitmapPixelFormat.Bgra8,   BitmapAlphaMode.Ignore,   (uint)renderTargetBitmap.PixelWidth,   (uint)renderTargetBitmap.PixelHeight,   logicalDpi,   logicalDpi,   pixelBuffer.ToArray());   await encoder.FlushAsync();   //Load and draw the bitmap image in PDF   PdfImage img = PdfImage.FromStream(stream.AsStream());   if (img.Width > img.Height)      document.PageSettings.Orientation = PdfPageOrientation.Portrait;          else                                                                                      document.PageSettings.Orientation = PdfPageOrientation.Landscape;       var section = document.Sections.Add();       section.PageSettings.Size = new SizeF(img.Width, img.Height);       page = section.Pages.Add();       page.Graphics.DrawImage(img, new RectangleF(0, 0, img.Width, img.Height));                                        }
查看完整描述

1 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

您可以直接將 pdf 文件保存在中Windows.Storage.ApplicationData.Current.LocalFolder并使用Windows.Data.Pdf 命名空間API 在應用程序中顯示 pdf,而不是調用Windows.System.Launcher.LaunchFileAsync(stFile)方法來啟動其他應用程序來打開 pdf 文件。

有關詳細信息,請參閱UWP PDF 文檔示例。


查看完整回答
反對 回復 2022-11-21
  • 1 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號