[英]Trying to save a pdf file of a screenshot of the form, given a specified location from the user's input
我正在尝试将创建的 PDF 文件保存到用户指定的位置。 我正在使用 Visual Studio Community 2019。本质上,我正在截取此表单的屏幕截图:
通过使用 PdfSharp 外部库,我创建了一个 PDF 文件,然后将该 PDF 保存到用户指定的某个文件位置。 这是用户选择其首选文件位置的 UI:
一旦程序尝试将 PDF 文件保存到用户指定的位置,就会出现问题。 这是我从 Visual Studio 得到的错误:
System.NotSupportedException: '没有可用于编码 1252 的数据。有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档。'
我在网上查找了这个特定的错误,但我并不真正理解它,也不知道如何处理它,这非常令人困惑。 在使用 Visual Basic 时,我仍然是一个初学者。 这是尝试执行此操作的代码:
Dim fileLocation As String
fileLocation = folderBrowseBox.Text
GetFormImage(True).Save(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg", ImageFormat.Jpeg)
' Create new pdf document and page
Dim doc As New PdfDocument()
Dim oPage As New PdfPage()
' Add the page to the pdf document and add the captured image to it
doc.Pages.Add(oPage)
Dim img As XImage = XImage.FromFile(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg")
'Create XImage object from file.
Using xImg = PdfSharp.Drawing.XImage.FromFile(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg")
'Resize page Width and Height to fit image size.
oPage.Width = xImg.PixelWidth * 72 / xImg.HorizontalResolution
oPage.Height = xImg.PixelHeight * 72 / xImg.HorizontalResolution
'Draw current image file to page.
Dim xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(oPage)
xgr.DrawImage(xImg, 0, 0, oPage.Width, oPage.Height)
End Using
doc.Save(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod))
img.Dispose()
倒数第二行代码(“ doc.Save(fileLocation & ...) ”)是发生错误的地方。 folderBrowseBox.Text (代码的第一行)来自您从我的第二个屏幕截图中看到的文本框。 任何帮助/建议将不胜感激!
在编写 PDF 文件之前尝试添加此行
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
从这里得到了想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.