c#

您所在的位置:网站首页 pages怎么查找 c#

c#

2022-12-23 05:59| 来源: 网络整理| 查看: 265

如何将xaml页面转换为pdf文件并在UWP应用程序内部的pdf查看器中显示? 时间:2019-04-01 10:36:16

标签: c# pdf uwp-xaml syncfusion pdf-viewer

我正在尝试使用Syncfusion PDF查看器,但要使用该方式,必须最终将xaml页另存为pdf文件。我该怎么办?

我点击了此链接,但没有得到想要的东西。 XAML to PDF Conversion

我正在尝试下面的代码从我的视图模型类中保存xaml页面[ new GenericManifestPDF(_manifestPDFDataModel); ],并出现以下错误:'值不在预期范围内范围” 等待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)); } //Save the document MemoryStream docStream = new MemoryStream(); document.Save(docStream); //Close the document document.Close(true); Save(docStream, "SampleManifest.pdf");

这是保存方法:

public async void Save(Stream stream, string filename) { stream.Position = 0; StorageFile stFile; if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))) { FileSavePicker savePicker = new FileSavePicker(); savePicker.DefaultFileExtension = ".pdf"; savePicker.SuggestedFileName = "Output"; savePicker.FileTypeChoices.Add("Adobe PDF Document", new List() { ".pdf" }); stFile = await savePicker.PickSaveFileAsync(); } else { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); } if (stFile != null) { Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite); Stream st = fileStream.AsStreamForWrite(); st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length); st.Flush(); st.Dispose(); fileStream.Dispose(); MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File created."); UICommand yesCmd = new UICommand("Yes"); msgDialog.Commands.Add(yesCmd); UICommand noCmd = new UICommand("No"); msgDialog.Commands.Add(noCmd); IUICommand cmd = await msgDialog.ShowAsync(); if (cmd == yesCmd) { // Launch the retrieved file bool success = await Windows.System.Launcher.LaunchFileAsync(stFile); } } }

这是我在上面提到的页面构造函数中绑定数据的方式:

public GenericManifestPDF(ManifestPDFDataModel PDFDataModel) { this.InitializeComponent(); Function.Text = "Function :" + PDFDataModel.Function; Counts.Text = "Count :" + PDFDataModel.Counts; PrintedValue.Text = "Printed :" + PDFDataModel.CreationDate; RouteValue.Text = "Route :" + PDFDataModel.Route; BatchIDValue.Text = "Batch :" + PDFDataModel.BatchID; BatchBarcodeText.Text = "Batch Barcode :" + PDFDataModel.Barcode; IBarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.CODE_39, Options = new ZXing.Common.EncodingOptions { Height = 100, Width = 450 } }; var result = writer.Write(PDFDataModel.Barcode); BarcodeImage.Source = result; PDFItemsList.ItemsSource = PDFDataModel.ItemsPDFList; }

这是页面的xaml代码:

我能够将其保存为pdf格式,但显示不正确,我认为我需要在此处进行一些更改

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); PdfPage page = section.Pages.Add(); page.Graphics.DrawImage(img, new RectangleF(0, 0, img.Width, img.Height)); }

我现在面临的唯一问题是如何根据列表视图项生成pdf页面。上面给出的代码每次仅生成一页,因此该列表不会完全可见。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将pdf文件直接保存在Windows.Storage.ApplicationData.Current.LocalFolder中,并使用Windows.Data.Pdf Namespace API在应用中显示pdf,而不用调用Windows.System.Launcher.LaunchFileAsync(stFile)方法来启动其他应用以打开pdf文件。

有关更多详细信息,请参见UWP PDF document sample。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3