簡體   English   中英

根據用戶輸入的指定位置,嘗試保存表單屏幕截圖的 pdf 文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM