2024年5月1日 星期三

[研究]ASP.NET,試用FreeSpire.PDF 10.2.0 取代 .pdf 中的文字

[研究]ASP.NET,試用FreeSpire.PDF 10.2.0 取代 .pdf 中的文字

2024-04-30

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

********************************************************************************

NuGet Gallery | FreeSpire.PDF
https://www.nuget.org/packages/FreeSpire.PDF

Free .NET PDF API - Developing PDF in C#, VB.NET, ASP.NET
https://www.e-iceblue.com/Introduce/free-pdf-component.html

授權 License

Free Spire.PDF for .NET is a Community Edition of the Spire.PDF for .NET, which is a totally free PDF API for commercial and personal use. As a standalone .NET library, Free Spire.PDF for .NET enables developers to create, write, edit, convert, print, handle and read PDF files on any .NET( C#, VB.NET, ASP.NET, .NET Core) applications.

Free version is limited to 10 pages of PDF. This limitation is enforced during writing PDF. When converting PDF to Image, XPS, Word, HTML, you can only get the first 3 pages of file. We don't provide technical or any other support to the users of the free versions.

********************************************************************************

續這篇,本篇談 FreeSpire.PDF 8.6.0 升級到 FreeSpire.PDF 10.2.0

10.2.0約2024年3月或4月釋出 ( NuGet 上寫 a month ago),非常新,語法似乎又改了。

[研究][C#][ASP.NET] 使用 Free Spire.PDF for NET v6.2.0 取代 .pdf 中的文字
https://shaurong.blogspot.com/2020/06/caspnet-free-spirepdf-for-net-v620-pdf.html

FreeSpire.PDF 8.6.0 升級 FreeSpire.PDF 10.2.0 後,Visual Studio 2019 編譯出現警告
  • warning CS0618: 'PdfTextFindCollection' 已經過時: 'This class may be removed in the future.'
  • warning CS0618: 'PdfPageBase.FindText(string, TextFindParameter)' 已經過時: 'This method may be removed in the future, please use PdfTextFinder.Find(string text) instead.'
  • warning CS0618: 'PdfTextFind' 已經過時: 'This class may be removed in the future.'
  • warning CS0618: 'PdfTextFind.Bounds' 已經過時: 'This property may be removed in the future, please use TextBounds instead.'
參考

********************************************************************************



參考

Default.aspx

<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF" OnClick="Button1_Click" />

Default.aspx.cs

using Spire.Pdf;
using Spire.Pdf.Texts;

protected void Button1_Click(object sender, EventArgs e)
{
 // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Replace all occurrences of target text with new text
            textReplacer.ReplaceAllText(".NET Framework", "New Content");
            textReplacer.ReplaceAllText("燒餅", "油條"); // 出錯了

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceTextInPage.pdf");

            // Dispose resources
            doc.Dispose();
}


直接拿官方範例來用,
textReplacer.ReplaceAllText(".NET Framework", "New Content");
改成用中文字,結果出錯了。
textReplacer.ReplaceAllText("燒餅", "油條"); // 出錯了

或者原來的 textReplacer.ReplaceAllText(".NET Framework", "New Content"); 保留,下面新增一行 textReplacer.ReplaceAllText("燒餅", "油條"); 結果原來的可以正常執行,但新增那行出錯。

System.NullReferenceException:'並未將物體參考

********************************************************************************

ASP.NET,WebForm, 下面是 FreeSpire.PDF 10.2.0 官方範例,

https://www.e-iceblue.com/Knowledgebase/Spire.PDF/Program-Guide/Text/Find-and-replace-text-on-PDF-document-in-C.html

把這行

textReplacer.ReplaceAllText(".NET Framework", "New Content");

改成用中文字,結果出錯了。

textReplacer.ReplaceAllText("燒餅", "油條"); // 出錯了


若原來的 textReplacer.ReplaceAllText(".NET Framework", "New Content"); 保留,

下面新增一行 textReplacer.ReplaceAllText("燒餅", "油條"); 

結果原來的那一行可以正常執行,但新增那一行出錯。


錯誤都是

System.NullReferenceException:'並未將物體參考


FreeSpire.PDF 10.2.0 的 ReplaceAllText 不支援中文字取代嗎 ? 還是要如何修改下面程式


using Spire.Pdf;

using Spire.Pdf.Texts;


protected void Button1_Click(object sender, EventArgs e)

{

 // Create a PdfDocument object

            PdfDocument doc = new PdfDocument();


            // Load a PDF file

            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");


            // Create a PdfTextReplaceOptions object

            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();


            // Specify the options for text replacement

            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;

            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;

            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;


            // Get a specific page

            PdfPageBase page = doc.Pages[0];


            // Create a PdfTextReplacer object based on the page

            PdfTextReplacer textReplacer = new PdfTextReplacer(page);


            // Set the replace options

            textReplacer.Options = textReplaceOptions;


            // Replace all occurrences of target text with new text

            textReplacer.ReplaceAllText(".NET Framework", "New Content");

            textReplacer.ReplaceAllText("燒餅", "油條"); // 出錯了


            // Save the document to a different PDF file

            doc.SaveToFile("ReplaceTextInPage.pdf");


            // Dispose resources

            doc.Dispose();

}

ChatGPT

這個問題可能是由於 FreeSpire.PDF 10.2.0 的 ReplaceAllText 方法無法正確處理中文文字所導致的。有幾個可能的解決方案:

  • 確認 ReplaceAllText 方法是否支援中文文字:查看官方文件或者其他資源,確定 ReplaceAllText 方法是否能夠處理中文文字。如果不支援,你可能需要採取其他方法來達到替換中文文字的目的。
  • 使用其他方法替換文字:如果 ReplaceAllText 方法無法處理中文文字,你可以考慮使用其他方法來替換文字,比如使用正則表達式進行替換。
  • 更新到最新版本:檢查是否有最新版本的 FreeSpire.PDF,有時更新版本可能修復了這樣的問題。
  • 聯繫支援:如果以上方法都無法解決問題,可以考慮聯繫 FreeSpire.PDF 的支援團隊,詢問他們關於中文文字替換的支援情況,或者是否有其他解決方案可供使用。

另外,我也注意到你在使用 ReplaceType 屬性時將其設置了多次,這樣只會保留最後一次的設置。你應該只設置一次 ReplaceType。

********************************************************************************

Copilot

FreeSpire.PDF for .NET 是一個功能強大且免費的 PDF API,可用於處理和操作 PDF 文件。不過,根據您的描述,您遇到了一個問題:在使用 textReplacer.ReplaceAllText("燒餅", "油條"); 時出現了 System.NullReferenceException 錯誤。

這個問題可能是由於 FreeSpire.PDF 10.2.0 的 ReplaceAllText 方法不支援中文字取代所導致的。您可以嘗試以下方法來解決這個問題:

確認文本存在:在替換文本之前,請確保您要替換的文本實際上存在於 PDF 文件中。您可以先使用 textReplacer.FindText("燒餅") 來查找文本,確認它是否存在。
使用 Unicode 編碼:中文字符需要使用 Unicode 編碼進行處理。您可以嘗試將 "燒餅" 和 "油條" 轉換為 Unicode 編碼,然後再進行替換。例如:

textReplacer.ReplaceAllText("\u70e7\u9915", "\u6cb9\u6a1e");

AI 產生的代碼。請仔細檢視及使用。 深入了解常見問題集。
其中 \u70e7 是 “燒” 的 Unicode 編碼,\u9915 是 “餅” 的 Unicode 編碼,\u6cb9 是 “油” 的 Unicode 編碼,\u6a1e 是 “條” 的 Unicode 編碼。
檢查文本替換選項:您在程式中設置了文本替換選項,請確保這些選項不會影響中文字符的替換。您可以嘗試移除以下行:

textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

AI 產生的代碼。請仔細檢視及使用。 深入了解常見問題集。
然後再次執行您的程式。
如果您仍然遇到問題,建議您查閱 FreeSpire.PDF 的官方文檔或諮詢其社區討論區,以獲得更詳細的幫助。123

********************************************************************************


懷疑不支援中文,或中文還需要額外的設定,暫時不知如何處理。

(待續)

相關

[研究]ASP.NET,試用FreeSpire.PDF 10.2.0 取代 .pdf 中的文字
https://shaurong.blogspot.com/2024/05/aspnetfreespirepdf-1020-pdf.html

[研究]ASP.NET,試用FreeSpire.PDF 8.6.0、10.2.0 產生 .pdf 檔案
https://shaurong.blogspot.com/2024/05/aspnetfreespirepdf-8601020.html

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 12.2.0 取代 .docx 中的文字
https://shaurong.blogspot.com/2024/05/caspnet-freespiredoc-1220-docx.html

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 12.2.0 把 .docx 轉成 .pdf
https://shaurong.blogspot.com/2024/05/caspnet-freespiredoc-1220-docx-pdf.html

[研究]FreeSpire.PDF 6.2.60 升級 7.8.9 後出錯,warning CS0618: 'PdfTextFind.Bounds' 已經過時: 'This property may be removed in the future, please use TextBounds instead.'
https://shaurong.blogspot.com/2021/08/warning-cs0618-pdftextfindbounds-this.html

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 取代 .docx 中的文字
https://shaurong.blogspot.com/2020/06/caspnet-freespiredoc-7110-docx.html

[研究][C#][ASP.NET] 使用 Free Spire.PDF for NET v6.2.0 取代 .pdf 中的文字
https://shaurong.blogspot.com/2020/06/caspnet-free-spirepdf-for-net-v620-pdf.html

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf
https://shaurong.blogspot.com/2020/06/caspnet-spiredoc-docx-pdf.html

[研究][C#][ASP.NET] 免費 Free Spire.PDF for .NET 3.2.0 試用 Url to pdf 檔案 (NuGet 安裝)
https://shaurong.blogspot.com/2018/03/aspnet-free-spirepdf-for-net-pdf.html

[研究][C#][ASP.NET] 免費 PDFsharp v1.32.3057.0 產生 pdf 試用 (NuGet 安裝)
https://shaurong.blogspot.com/2018/03/caspnet-pdfsharp-v13230570-pdf-nuget.html

沒有留言:

張貼留言