我正在嘗試使用 Syncfusion PDF 查看器,但為了使用它,我最終必須將 xaml 頁(yè)面保存為 pdf 文件。我怎么做?我點(diǎn)擊了此鏈接,但它沒有給我想要的東西。XAML 到 PDF 轉(zhuǎn)換我正在嘗試使用下面的代碼來保存 xaml 頁(yè)面 [ new GenericManifestPDF(_manifestPDFDataModel); ] 從我的視圖模型類中得到這個(gè)錯(cuò)誤:' 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)); }
如何將 xaml 頁(yè)面轉(zhuǎn)換為 pdf 文件并在 UWP 應(yīng)用程序內(nèi)的 pdf 查看器中顯示?
喵喵時(shí)光機(jī)
2022-11-21 22:06:27