【问题标题】:Trying to save a pdf file of a screenshot of the form, given a specified location from the user's input尝试保存表单屏幕截图的pdf文件,给定用户输入的指定位置
【发布时间】:2021-06-18 15:49:46
【问题描述】:

我正在尝试将创建的 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(第一行代码)来自您在我的第二个屏幕截图中看到的文本框。任何帮助/建议将不胜感激!

【问题讨论】:

  • RemoveWhitespace()filenamecollectionPeriod 定义在哪里?
  • @ÉtienneLaneville RemoveWhitespace() 是我在上面的帖子中看到的代码之外的一个函数。基本上,您从我的帖子中看到的所有代码都发生在用户单击“2) 创建 PDF”按钮时。我没有包括那部分,因为我担心我的帖子会太长/太无聊,我想直奔主题。 filenamecollectionPeriod 是我在“2)创建 PDF”按钮单击事件中声明为字符串的变量;它们被设置为第一个屏幕截图中“公司/客户名称”和“收集期”旁边的文本框的输入。

标签: vb.net pdfsharp


【解决方案1】:

在编写 PDF 文件之前尝试添加这一行

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

here得到这个想法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多