如何在 C# 中展平 PDF 表單字段?Aspose.PDF幾行代碼幫助回答這個問題

當您想將 PDF 表單字段與 .NET 應用程序中的內容合併,因此這些字段不可編輯時,您想知道如何在 C# 代碼中拼合 PDF 表單字段?Aspose.PDF for .NET API用幾行代碼幫助回答這個問題。

使用此 .NET API,可以一次展平基於 acroform 的PDF、所有表單字段或選擇特定字段以在您的 C# 應用程序中展平。ASP.NET Web 應用程序、Windows 桌面應用程序或其他 .NET 平臺的代碼相同。

Aspose.PDF for .NET是用於.NET Core的高級PDF處理API,可以在跨平臺應用程序中輕鬆生成,修改,轉換,呈現,保護和打印文檔。無需使用Adobe Acrobat。(下載Aspose.PDF安裝包

在 C# 中展平 PDF 表單字段的步驟

  1. 爲 .NET NuGet 包安裝Aspose.PDF
  2. 包括Aspose.PDF和Aspose.PDF.Facades命名空間
  3. 使用許可證類設置 Aspose 許可證
  4. 創建 Form 類的實例
  5. 綁定要拼合的 PDF 文件
  6. 使用FlattenAllField 方法合併所有表單域
  7. 使用 FlattenField 方法展平特定字段
  8. 最後,保存拼合後的輸出PDF文件

在 C# 中展平 PDF 表單字段的代碼

代碼如下:

using System;
//Add Aspose.Pdf for .NET package reference
//Use following namespaces to flatten PDF form Fields
using Aspose.Pdf;
using Aspose.Pdf.Facades;


namespace FlattenPDFFormFields
{
    class Program
    {
        static void Main(string[] args)
        {

            //Set Aspose license before flattening PDF form fields
            //with the help of Aspose.Pdf for .NET 
            Aspose.Pdf.License AsposePDFLicense = new Aspose.Pdf.License();
            AsposePDFLicense.SetLicense(@"c:\asposelicense\license.lic");

            //create an object of Form class to initiate form field flattening 
            Form PDFFormToBeFlattened = new Form();

            //Bind the PDF file which contains the form fields to be flattened
            PDFFormToBeFlattened.BindPdf("SamplePDFFormwithFields.pdf");

            //Use FlattenAllFields method to flatten all of the fields in the 
            //PDF form loaded above
            PDFFormToBeFlattened.FlattenAllFields();

            //Save output PDF file with form fields flattened using Save method 
            //of Form object
            PDFFormToBeFlattened.Save("OutputPDFwithFlattenedFormFields.pdf");
        }
    }
}

以下 C# 示例使用 FlattenField 方法將特定字段與 PDF 文件合併。通過提供字段名稱作爲參數,您可以使用此方法展平任意數量的字段。

在 C# 中展平單個 PDF 表單字段的代碼

using System;
//Add Aspose.Pdf for .NET package reference
//Use following namespaces to flatten PDF form Fields
using Aspose.Pdf;
using Aspose.Pdf.Facades;


namespace FlattenPDFFormFields
{
    class Program
    {
        static void Main(string[] args)
        {

            //Set Aspose license before flattening PDF form fields
            //with the help of Aspose.Pdf for .NET 
            Aspose.Pdf.License AsposePDFLicense = new Aspose.Pdf.License();
            AsposePDFLicense.SetLicense(@"c:\asposelicense\license.lic");

            //create an object of Form class to initiate form field flattening 
            Form PDFFormToBeFlattened = new Form();

            //Bind the PDF file which contains the form fields to be flattened
            PDFFormToBeFlattened.BindPdf("SamplePDFFormwithFields.pdf");

            //Use FlattenField method and specify the name of the particular field
            //when flattening only a single form field
            PDFFormToBeFlattened.FlattenField("TextFormField");

            //Save output PDF file with form fields flattened using Save method 
            //of Form object
            PDFFormToBeFlattened.Save("OutputPDFwithFlattenedFormFields.pdf");
        }
    }
}

請注意,在 C# 中拼合 PDF 表單字段時,您不需要Adobe Acrobat或任何其他 PDF 編輯軟件。此 API 包含在 .NET 中以編程方式展平 pdf 所需的一切。

如果您仍然不清楚如何在 C# 中展平 PDF 表單字段, 可加入Aspose資源分享交流羣(761297826)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章