顯示具有 FreeSpire 標籤的文章。 顯示所有文章
顯示具有 FreeSpire 標籤的文章。 顯示所有文章

2025年10月13日 星期一

[研究]ASP.NET WebForm 使用 Spire.Pdf ( FreeSpire.PDF 8.6.0) 套件,文字增加底色

[研究]ASP.NET WebForm 使用 Spire.Pdf ( FreeSpire.PDF 8.6.0) 套件,文字增加底色

2025-10-13

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

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


foreach (PdfTextFind find in page.FindText("DD – MM - 2025", TextFindParameter.IgnoreCase).Finds)
{
    rec = find.TextBounds[0];

    page.Canvas.DrawRectangle(PdfBrushes.Black, rec); 
    rec.Width = PdfPageSize.A4.Width - rec.X * 2;
    rec.Height = rec.Y + 20;
    string date = DateTime.Now.ToString("dd-MM-yyyy");
    page.Canvas.DrawString(date, new PdfTrueTypeFont(new Font("微軟正黑體", 14, FontStyle.Regular), true), brush, rec);   
}



ChatGPT

foreach (PdfTextFind find in page.FindText("DD – MM - 2025", TextFindParameter.IgnoreCase).Finds)
{
    rec = find.TextBounds[0];

    // 建立指定色碼 (#38B394) 的刷子
    PdfBrush customBrush = new PdfSolidBrush(new PdfRGBColor(0x38, 0xB3, 0x94));

    // 畫底色
    page.Canvas.DrawRectangle(customBrush, rec);

    // 調整繪製區域寬高
    rec.Width = PdfPageSize.A4.Width - rec.X * 2;
    rec.Height = rec.Y + 20;

    // 寫入新的日期文字
    string date = DateTime.Now.ToString("dd-MM-yyyy");
    page.Canvas.DrawString(
        date,
        new PdfTrueTypeFont(new Font("微軟正黑體", 14, FontStyle.Regular), true),
        PdfBrushes.White, // 若底色深,字用白色會更清楚
        rec
    );
}

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

2025-10-14 補充

一、這個方法預設沒有底色。也就是說,除非你額外在文字後面繪製了矩形或指定背景筆刷,否則文字是「透明背景」直接疊在 PDF 的頁面上。

page.Canvas.DrawString(...)  


二、目前的程式碼會造成「有底色」的原因

page.Canvas.DrawRectangle(PdfBrushes.Black, rec);   
  

這一行其實會先在該區域畫一個「黑色實心矩形」。

所以當你後面再 DrawString() 畫文字時,視覺上就像文字有「黑底」。

這並不是 DrawString 預設有底色,而是你手動畫出來的。


三、讓底色變透明的方式

foreach (PdfTextFind find in page.FindText("DD – MM - 2025", TextFindParameter.IgnoreCase).Finds)
{
    RectangleF rec = find.TextBounds[0];
    string date = DateTime.Now.ToString("dd-MM-yyyy");

    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("微軟正黑體", 14, FontStyle.Regular), true);
    PdfSolidBrush brush = new PdfSolidBrush(Color.Black); // 文字顏色

    page.Canvas.DrawString(date, font, brush, rec);
}


實際測試,底色透明後,原來想替換的字會顯示出來,造成文字重疊。

Spire.PDF 的 FindText() 雖能找到文字區域,但不會刪除原文字物件。

page.Canvas.DrawString() 只是繪製新內容,無法覆蓋或刪除原字元。

要達成「替換文字」的視覺效果,你通常需要遮蓋原文字再寫新字。

PdfPage.ReplaceText(oldText, newText) 只有 Spire.PDF Pro 付費版 才支援;

免費版或標準版只能用上面「畫矩形 + 重畫文字」的方式模擬替換。


(完)

相關

[研究]FreeSpire.PDF 10.2.0和中文相容有問題,Spire.PDF 10.10.5 或 FreeSpire.PDF 8.6.0正常

https://shaurong.blogspot.com/2024/10/freespirepdf-1020spirepdf-10105.html


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;

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

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

(待續)

相關

[研究]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

[研究]ASP.NET,試用FreeSpire.PDF 8.6.0升級10.2.0 產生 .pdf 檔案

[研究]ASP.NET,試用FreeSpire.PDF 8.6.0升級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.

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




Default.aspx

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

Default.aspx.cs

using Spire.Pdf;    // PdfDocument 使用
using Spire.Pdf.Graphics;   // PdfFont 使用
using System;
using System.Drawing;   // PointF 使用

protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
    // 創建 PDF 文件
    PdfDocument doc = new PdfDocument();

    // 添加頁面
    PdfPageBase page = doc.Pages.Add();

    // 繪製文字到頁面
    page.Canvas.DrawString("Hello, World!", new PdfFont(PdfFontFamily.Helvetica, 20f), PdfBrushes.Black, new PointF(100, 100));

    // 儲存 PDF
    string filePath = Server.MapPath("~/GeneratedPDFs/Example.pdf");
    doc.SaveToFile(filePath);
    doc.Close();

    // 下載 PDF
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=Example.pdf");
    Response.TransmitFile(filePath);
    Response.End();
}




實際測試,FreeSpire.PDF 8.6.0 此測試程式可用,

把 FreeSpire.PDF 8.6.0 升級到 FreeSpire.PDF 10.2.0 後,也可使用。

(完)

相關

[研究]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

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 12.2.0 取代 .docx 中的文字

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 12.2.0 取代 .docx 中的文字

2024-05-01

Visual Studio 2019 v16.6.1 + WebForm + WebApplication
Nuget 安裝 FreeSpire.Doc 7.11.0 元件 (不是 Spire.Doc )

官方網站

免費版有篇幅限制。在加載或操作 Word 文檔時,要求 Word 文檔不超過 500 個段落,25 個表格。同時將 Word 文檔轉換為 PDF 和 XPS 格式時,僅支持轉換前三頁。

Free Spire.Doc for .NET is a Community Edition of the Spire.Doc for .NET, which is a totally free word API for commercial and personal use. As a free C#/VB.NET API, it also offers a lot of powerful functions. Developers can use it to generate, read, write, save and convert documents on any .NET applications. The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013 and Word 2016) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc.

Friendly Reminder:
Free version is limited to 500 paragraphs and 25 tables. This limitation is enforced during reading or writing files. When converting word documents to PDF and XPS files, you can only get the first 3 page of PDF file. Upgrade to Commercial Edition of Spire.Doc

參考
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html

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


WebApplication1

正在安裝:

System.Buffers.4.4.0
System.Numerics.Vectors.4.4.0
System.Runtime.CompilerServices.Unsafe.4.5.2
System.Memory.4.5.3
HarfBuzzSharp.2.6.1.5
FreeSpire.Doc.12.2.0

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

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpireDocDemo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using Spire.Doc;
using System;

namespace SpireDocDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Document document = new Document();

            document.LoadFromFile(@"C:\Temp\test.docx"); 
            document.Replace("Hello", "HHeelllloo", false, true);

            document.SaveToFile(@"C:\Temp\test2.docx", FileFormat.Docx);
        }
    }
}






實際測試,FreeSpire.Doc 7.11.0 和 FreeSpire.Doc 12.2.0 都可以使用。

(完)

相關

[研究]ASP.NET,試用FreeSpire.PDF 8.6.0、10.2.0
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

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 12.2.0 把 .docx 轉成 .pdf

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 12.2.0 把 .docx 轉成 .pdf

2024-05-01

Visual Studio 2019 v16.6.1 + WebForm + WebApplication
Nuget 安裝 FreeSpire.Doc 7.11.0 元件 (不是 Spire.Doc )

官方網站

免費版有篇幅限制。在加載或操作 Word 文檔時,要求 Word 文檔不超過 500 個段落,25 個表格。同時將 Word 文檔轉換為 PDF 和 XPS 格式時,僅支持轉換前三頁。

Free Spire.Doc for .NET is a Community Edition of the Spire.Doc for .NET, which is a totally free word API for commercial and personal use. As a free C#/VB.NET API, it also offers a lot of powerful functions. Developers can use it to generate, read, write, save and convert documents on any .NET applications. The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013 and Word 2016) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc.

Friendly Reminder:
Free version is limited to 500 paragraphs and 25 tables. This limitation is enforced during reading or writing files. When converting word documents to PDF and XPS files, you can only get the first 3 page of PDF file. Upgrade to Commercial Edition of Spire.Doc

參考
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html

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


WebApplication1

正在安裝:

System.Buffers.4.4.0
System.Numerics.Vectors.4.4.0
System.Runtime.CompilerServices.Unsafe.4.5.2
System.Memory.4.5.3
HarfBuzzSharp.2.6.1.5
FreeSpire.Doc.12.2.0

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

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpireDocDemo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using Spire.Doc;
using System;

namespace SpireDocDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"C:\temp\test.docx");

            //Convert Word to PDF
            document.SaveToFile(@"C:\temp\testt.pdf", Spire.Doc.FileFormat.PDF);

            //Launch Document
            System.Diagnostics.Process.Start(@"C:\temp\testt.pdf");
        }
    }
}





實際測試,FreeSpire.Doc 7.11.0 和 FreeSpire.Doc 12.2.0 都可以使用。

(完)

相關

[研究]ASP.NET,試用FreeSpire.PDF 8.6.0、10.2.0
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

2021年8月27日 星期五

[研究]FreeSpire.PDF 6.2.60 升級 7.8.9 後出錯,warning CS0618: 'PdfTextFind.Bounds' 已經過時: 'This property may be removed in the future, please use TextBounds instead.'

[研究]FreeSpire.PDF 6.2.60 升級 7.8.9 後出錯,warning CS0618: 'PdfTextFind.Bounds' 已經過時: 'This property may be removed in the future, please use TextBounds instead.'

2021-08-26

今天 Visual Studio 2019 編譯某方案時,出現

warning CS0618: 'PdfTextFind.Bounds' 已經過時: 'This property may be removed in the future, please use TextBounds instead.'

似乎是 NuGet 升級 PdfTextFind 版本的關係。packages.conf 顯示目前為 FreeSpire.PDF 7.8.9

PdfTextFind Class
Assembly: Spire.Pdf (in Spire.Pdf.dll) Version: 6.2.6.0 (6.2.6.2020)
https://www.e-iceblue.com/api_documents/5e4e0b69721393-11731598/res/html/d337da17-db94-256c-1fe8-b08d2796720c.htm

官方網頁說明依然 6.2.60 版,依然是用 Bounds,沒有 TextBounds 用法。找了一下

 rec = find.Bounds;

要改成

rec = find.TextBounds[0];

(完)

2020年6月30日 星期二

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 取代 .docx 中的文字

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 取代 .docx 中的文字

2020-06-30

Visual Studio 2019 v16.6.2 + ASP.NET + WebForm + WebApplication

用 NuGet 安裝 FreeSpire.Doc 7.11.0

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpireDocReplaceText.Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs
using Spire.Doc;
using System;

namespace SpireDocReplaceText
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Document document = new Document();
            
            document.LoadFromFile(@"C:\Temp\樣張1.docx"); //失敗
            //document.LoadFromFile(@"C:\Temp\test1.docx"); //成功

            document.Replace("{$大學}", "測試大學", false, true);

            document.SaveToFile(@"C:\Temp\樣張1R.docx", FileFormat.Docx);
            //System.Diagnostics.Process.Start(@"C:\Temp\樣張1R.docx");
        }
    }
}

其中 test1.docx 是自己建立的,檔案中只有單純的文字。
樣張1.docx 別人給的,內容較複雜,但是Word開啟該檔案,用 "{$大學}"去搜尋,是可以找到的。

表示 Spire.Doc 無法適用於各種 .docx 檔案。

(完)

2020年6月15日 星期一

[研究][C#][ASP.NET] 使用 Free Spire.PDF for NET v6.2.0 取代 .pdf 中的文字

[研究][C#][ASP.NET] 使用 Free Spire.PDF for NET v6.2.0 取代 .pdf 中的文字

2020-06-13
2020-06-15  補充
2020-06-21 更新
2020-06-30 更新

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

免費版限制
https://www.e-iceblue.com/Download/download-pdf-for-net-free.html
免費版本可以商業使用或個人使用,但有 10 頁 pdf 限制 (讀取 or 建立檔案),若把 .pdf 轉成圖片,只能轉3頁。要更強功能,請用商業版。

Thank you for your interests in our Free Spire.PDF for .NET. It is totally free without any warning message for your commercial and personal use. Free version is limited to 10 pages of PDF. This limitation is enforced during loading and creating files. When converting PDF to Image, the first 3 pages of PDF files will be converted to Image format successfully.

If your requirements exceed the limitation, please use our Commercial Edition of Spire.PDF.

Visual Studio 2019 v16.6.2 + C# + ASP.NET + WebForm + WebApplication
NuGet 要安裝 Free Spire.PDF for NET v6.2.0 

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ReplaceTextbySpirePDF.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;

namespace ReplaceTextbySpirePDF
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // https://www.e-iceblue.com/Knowledgebase/Spire.PDF/Program-Guide/Text/Find-and-replace-text-on-PDF-document-in-C.html
            // Step 1: Load the sample document file.

            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Temp\樣張-2020-06-09.pdf");

            // Step 2: Searched the text “Spire.PDF for .NET” from the first page of the sample document.

            PdfPageBase page = doc.Pages[0];
            PdfTextFindCollection collection = page.FindText("{@大學}", TextFindParameter.IgnoreCase);

            // Step 3: Use the new string “E - iceblue Spire.PDF” to replace the searched text and sent the font and color for the new string.

            String newText = "測試大學";
            //String newText = "ABC";
            //Creates a brush
            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
            
            //Defines a font
            // 中文字亂碼,若 newText 為英文字可用,但英文 12f 改 14f, 16f, 18f 可用,但 20f, 22f 會空白
            //PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 18f, FontStyle.Regular));    

            //【Spire.PDF】出现中文乱码,中文显示不正常的情况
            // https://blog.csdn.net/weixin_38211198/article/details/89192598
            //Font font0 = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point, 1, true);    
            //Font font0 = new Font("DFKai-SB", 10, FontStyle.Regular, GraphicsUnit.Point, 1, true);
            //Font font0 = new Font("DFKai-SB", 22, FontStyle.Regular, GraphicsUnit.Point, 1, true); 
            //Font font0 = new Font("DFKai-SB", 22, FontStyle.Regular); 
            Font font0 = new Font("標楷體", 18, FontStyle.Bold);   // 中文字體 不可超過18,19開始會變成空白
            //Font font0 = new Font("新細明體", 19, FontStyle.Regular);   // 英文字、中文字變成空白
            PdfTrueTypeFont font = new PdfTrueTypeFont(font0, true);   // 標楷體,中文字亂碼,true 表示 unicode

            RectangleF rec;
            foreach (PdfTextFind find in collection.Finds)
            {
                // Gets the bound of the found text in page
                rec = find.Bounds;
                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                page.Canvas.DrawString(newText, font, brush, rec);
                // https://www.e-iceblue.cn/spirepdf/set-font-for-pdf.html
                //page.Canvas.DrawString(newText, font, brush, new PointF(50,50));
            }

            // Step 4: Save the document to file.

            doc.SaveToFile(@"C:\Temp\樣張-2020-06-15.pdf");
        }
    }
}

若要彈出 pdf 下載對話盒視窗

Response.Clear();
Response.Buffer = true;
Response.Charset = "";
string mainFileName = "主要檔案名稱";
string dateTimeString = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff");

// 若檔案名稱有中文字或特殊符號,Response.AddHeader() 輸出檔案名稱要編碼
string outFileName = HttpUtility.UrlEncode(mainFileName, System.Text.Encoding.UTF8) + "-" +
    dateTimeString + ".pdf";

// XLWorkbook.SaveAs() 輸出檔案名稱不要用 HttpUtility.UrlEncode 編碼
// Response.TransmitFile() 輸出檔案名稱不要用 HttpUtility.UrlEncode 編碼
string dirFileName = @"C:\TEMP\" + mainFileName + "-" +
    dateTimeString + ".pdf";

Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("content-disposition", "attachment;filename=" + outFileName);
Response.TransmitFile(dirFileName);
Response.Flush();
Response.End();

中文字的問題,可參考這裡。
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Text/How-to-add-simplified-and-traditional-Chinese-characters-to-PDF-in-C-VB.NET.html

因為字體只能小於等於 18,若超過,可能要結合下面2篇去產生;或者有辦法用  Free Spire.PDF for NET v6.2.0  直接替換字型大小 18 的字。

[研究][C#][ASP.NET] 用 NPOI 替換 Word (.docx) 中的文字

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf

********************************************************************************
2020-06-15  補充

發現字體大小限制問題問題來自 rec 的範圍大小,限制了文字字串可用的高度、寬度,上面程式 rec 是根據被替換的字 {@大學} 計算大小,所以字大些就放不下。程式片段改成如下(整段文字組合好,把幾個字,換成整段文字;不是把幾個字只取代成幾個字。


foreach (PdfTextFind find in page.FindText("{$茲證明}", TextFindParameter.IgnoreCase).Finds)
            {
                rec = find.Bounds;
                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                rec.Width = PdfPageSize.A4.Width - rec.X *2;
                rec.Height = rec.Y + 72;   // 72 是自己測試隨便放的值,高度夠就好。
                page.Canvas.DrawString("茲證明那一整串字。", new PdfTrueTypeFont(new Font("標楷體", 22, FontStyle.Bold), true), brush, rec );
            }
foreach (PdfTextFind find in page.FindText("{$中華民國}", TextFindParameter.IgnoreCase).Finds)
            {
                rec = find.Bounds;
                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                rec.Width = PdfPageSize.A4.Width - rec.X * 2;
                rec.Height = rec.Y + 20;
                string date = "中\t華\t民\t國\t" + (DateTime.Now.Year - 1911).ToString() + "\t年\t" + DateTime.Now.Month.ToString() + "\t月\t" + DateTime.Now.Day.ToString() + "\t日";
                page.Canvas.DrawString(date, new PdfTrueTypeFont(new Font("標楷體", 14, FontStyle.Bold), true), brush, rec);
            }


********************************************************************************
2020-06-30  補充

不要把 .PDF 當成是 .docx,如果換上去的文字很長,可能會和其他文字重疊顯示。
因為 .pdf 產生時,會明確定義文字或圖片呈現是格式和位置,其中某段落文字被更換後,其他段落文字位置不會因為某段落文字增加變長,下面的段落自動下移動,不會的,請把空間留夠。

********************************************************************************
2023-11-07 補充,關於左右對齊,實際測試失敗


PdfStringFormat format = new PdfStringFormat();
format.Alignment= PdfTextAlignment.Justify; // 只有 Center, Left, Right, Justify,實際測試 Justify 無法左右對齊
//string text = "這是您想要兩側對齊的文本。";
//page.Canvas.DrawString(text, font, PdfBrushes.Black, new RectangleF(100, 100, 400, 0), format);

page.Canvas.DrawRectangle(PdfBrushes.White, rec);
rec.Width = PdfPageSize.A4.Width - rec.X * 2;
rec.Height = rec.Y + 72;   // 72 是自己測試隨便放的值,高度夠就好。
//page.Canvas.DrawString(content, new PdfTrueTypeFont(new Font("微軟正黑體", 18, FontStyle.Bold), true), brush, rec);
page.Canvas.DrawString(content, new PdfTrueTypeFont(new Font("微軟正黑體", 18, FontStyle.Bold), true), brush, rec, format);


********************************************************************************
2021-08-26

FreeSpire.PDF 6.2.60 升級 FreeSpire.PDF 7.8.9 後,Visual Studio 2019 編譯出現警告

warning CS0618: 'PdfTextFind.Bounds' 已經過時: 'This property may be removed in the future, please use TextBounds instead.'

似乎是 NuGet 升級 PdfTextFind 版本的關係

PdfTextFind Class
Assembly: Spire.Pdf (in Spire.Pdf.dll) Version: 6.2.6.0 (6.2.6.2020)
https://www.e-iceblue.com/api_documents/5e4e0b69721393-11731598/res/html/d337da17-db94-256c-1fe8-b08d2796720c.htm

官方網頁說明依然 6.2.60 版,依然是用 Bounds,沒有 TextBounds 用法。找了一下

 rec = find.Bounds;

要改成

rec = find.TextBounds[0];
********************************************************************************
2024-05-01

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.'
參考

直接拿官方範例來用,但底下多加一行
textReplacer.ReplaceAllText(".NET Framework", "New Content");
textReplacer.ReplaceAllText("燒餅", "油條"); // 出錯了


不知是否因為替換的字是繁體中文字的關係? 或要做特別的處理。

待研究。

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

(完)

相關

[研究][C#][ASP.NET] 使用 Free Spire.PDF for NET v6.2.0 取代 .pdf 中的文字 (成功,要考慮文字重疊問題)

[研究][C#][ASP.NET]使用 Aspose.PDF v20.6.0 替換 .pdf 中文字 (評估版產生的 .pdf 會有紅字,商業版疑要不斷更新授權)

[研究][C#][ASP.NET] 使用 iText 7 取代 .pdf 中的文字 (失敗)

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

[研究][C#][ASP.NET] 用 Aspose.Words for .NET v20.6.0 取代 .docx 中文字 (目前正常)

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 取代 .docx 中的文字 (成敗未定)

[研究][C#][ASP.NET] 用 NPOI 替換 Word (.docx) 中的文字 (成敗未定)

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

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf (免費版只能轉前3頁)

[研究][C#][ASP.NET] 用 Aspose.Words for .NET v20.6.0 把 .docx 轉 .pdf

2020年6月10日 星期三

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf

2020-06-10

Visual Studio 2019 v16.6.1 + WebForm + WebApplication
Nuget 安裝 FreeSpire.Doc 7.11.0 元件 (不是 Spire.Doc )

官方網站

免費版有篇幅限制。在加載或操作 Word 文檔時,要求 Word 文檔不超過 500 個段落,25 個表格。同時將 Word 文檔轉換為 PDF 和 XPS 格式時,僅支持轉換前三頁。

Free Spire.Doc for .NET is a Community Edition of the Spire.Doc for .NET, which is a totally free word API for commercial and personal use. As a free C#/VB.NET API, it also offers a lot of powerful functions. Developers can use it to generate, read, write, save and convert documents on any .NET applications. The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013 and Word 2016) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc.

Friendly Reminder:
Free version is limited to 500 paragraphs and 25 tables. This limitation is enforced during reading or writing files. When converting word documents to PDF and XPS files, you can only get the first 3 page of PDF file. Upgrade to Commercial Edition of Spire.Doc

參考
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpireDocDemo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using Spire.Doc;
using System;

namespace SpireDocDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"C:\temp\test.docx");

            //Convert Word to PDF
            document.SaveToFile(@"C:\temp\testt.pdf", FileFormat.PDF);

            //Launch Document
            System.Diagnostics.Process.Start(@"C:\temp\testt.pdf");
        }
    }
}


(完)

2018年3月14日 星期三

[研究][C#][ASP.NET] 免費 Free Spire.PDF for .NET 3.2.0 試用 Url to pdf 檔案 (NuGet 安裝)

[研究][C#][ASP.NET] 免費 Free Spire.PDF for .NET 3.2.0 試用 Url to pdf 檔案 (NuGet 安裝)

2018-03-14

免費版的 Spire.PDF 如果產生的 .pdf 會超過 10 頁就會出現錯誤。

Spire.PDF for .NET
https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html
Spire.PDF for .NET is a professional PDF component applied to creating, writing, editing, handling and reading PDF files without any external dependencies within .NET application. Using this .NET PDF library, you can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely through C#/VB.NET without installing Adobe Acrobat.

Free Spire.PDF for .NET
https://www.e-iceblue.com/Introduce/free-pdf-component.html
Free Spire.PDF for .NET is a Community Edition of the Spire.PDF for .NET, which is a totally free PDF component for commercial and personal use. As a standalone C#/VB.NET component, Free Spire.PDF for .NET enables developers to create, write, edit, convert, print, handle and read PDF files on any .NET applications.

價格
https://www.e-iceblue.com/Buy/Spire.PDF.html
Spire.PDF Pro Edition Developer Subscription
$599.00 1 Developer & 1 Deployment Location

Spire.PDF Pro Edition Developer OEM Subscription
$1 799.00 1 Developer & Unlimited Deployment Locations

Spire.PDF Pro Edition Site Enterprise Subscription
$3 070.00 Unlimited Developers & Up to 10 Deployment Locations

Spire.PDF Pro Edition Site OEM Subscription
$6 980.00 Unlimited Developers & Unlimited Deployment Locations

Spire PDF for .NET 在ASP.NET中的使用 ---- 并非那么“美好”,有些挫折!
2015-03-06
https://www.bbsmax.com/A/GBJrY2a3z0/












參考範例

How to convert HTML into PDF in C#, VB.NET
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpirePDFTest.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Spire.Pdf;

namespace SpirePDFTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            //String url = "http://www.london2012.com/news/articles/paralympic-torch-relay-route-revealed-1258473.html";
            String url = "http://www.hinet.net";
            Thread thread = new Thread(() =>
            {
                doc.LoadFromHTML(url, false, true, true);
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            //Save pdf file.
            doc.SaveToFile("sample.pdf");
            doc.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("sample.pdf");
        }
    }
}



(下圖) 免費版的 Spire.PDF 如果產生的 .pdf 會超過 10 頁就會出現錯誤。

(完)

相關

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

[研究][C#][ASP.NET] SelectPdf 社群版 - 免費 HTML to PDF v17.3.0 的 Url to Pdf 試用 (NuGet 安裝)
http://shaurong.blogspot.com/2018/03/caspnet-selectpdf-html-to-pdf-url-to-pdf.html

[研究][C#][ASP.NET] iTextSharp v7.1.1 產生 pdf 試用 (NuGet 安裝)
http://shaurong.blogspot.com/2018/03/caspnet-itextsharp-v711-pdf-nuget.html

[研究][C#][ASP.NET] iTextSharp v5.5.13 產生 pdf 試用 (NuGet 安裝)
http://shaurong.blogspot.com/2018/03/caspnet-itextsharp-v5513-pdf-nuget.html

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