我正在嘗試使用 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 文檔示例。
- 1 回答
- 0 關注
- 141 瀏覽
添加回答
舉報
0/150
提交
取消