根据用户输入的指定位置,尝试保存表单屏幕截图的 pdf 文件

alk*_*458 -1 vb.net pdfsharp

我正在尝试将创建的 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()
Run Code Online (Sandbox Code Playgroud)

倒数第二行代码(“ doc.Save(fileLocation & ...) ”)是发生错误的地方。folderBrowseBox.Text (第一行代码)来自您在我的第二个屏幕截图中看到的文本框任何帮助/建议将不胜感激!

小智 5

尝试在写入 PDF 文件之前添加此行

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)

从这里得到的想法