• <menu id="w2i4a"></menu>
  • logo Aspose.PDF for .NET開(kāi)發(fā)者使用教程

    文檔首頁(yè)>>Aspose.PDF for .NET開(kāi)發(fā)者使用教程>>PDF管理控件Aspose.PDF for .Net使用教程(四十二):在PDF文檔中添加、修改和刪除表單字段

    PDF管理控件Aspose.PDF for .Net使用教程(四十二):在PDF文檔中添加、修改和刪除表單字段


    Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。

    在接下來(lái)的系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何在PDF文檔中添加、修改和刪除表單字段。

    >>Aspose.PDF for .NET更新至最新版v20.7,歡迎下載體驗(yàn)。


    在PDF文檔中添加表單字段

    要添加表單字段:

    • 創(chuàng)建您要添加的表單字段。
    • 調(diào)用Form集合的Add方法。
    添加TextBoxField

    以下示例顯示了如何添加TextBoxField。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    // Open document
    Document pdfDocument = new Document(dataDir + "TextField.pdf");
    
    // Create a field
    TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 200, 300, 300));
    textBoxField.PartialName = "textbox1";
    textBoxField.Value = "Text Box";
    
    // TextBoxField.Border = new Border(
    Border border = new Border(textBoxField);
    border.Width = 5;
    border.Dash = new Dash(1, 1);
    textBoxField.Border = border;
    
    textBoxField.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
    
    // Add field to the document
    pdfDocument.Form.Add(textBoxField, 1);
    
    dataDir = dataDir + "TextBox_out.pdf";
    // Save modified PDF
    pdfDocument.Save(dataDir);
    添加RadioButtonField

    以下代碼段顯示了使用三個(gè)選項(xiàng)添加RadioButtonField并將其放置在Table單元格中的步驟。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    // Instantiate Document object
    Document pdfDocument = new Document();
    // Add a page to PDF file
    pdfDocument.Pages.Add();
    // Instatiate RadioButtonField object with page number as argument
    RadioButtonField radio = new RadioButtonField(pdfDocument.Pages[1]);
    // Add first radio button option and also specify its origin using Rectangle object
    radio.AddOption("Test", new Rectangle(0, 0, 20, 20));
    // Add second radio button option
    radio.AddOption("Test1", new Rectangle(20, 20, 40, 40));
    // Add radio button to form object of Document object
    pdfDocument.Form.Add(radio);
    
    dataDir = dataDir + "RadioButton_out.pdf";
    // Save the PDF file
    pdfDocument.Save(dataDir);

    以下代碼段顯示了如何在PDF文檔中添加RadioButtonField。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    Document doc = new Document();
    Page page = doc.Pages.Add();
    Aspose.Pdf.Table table = new Aspose.Pdf.Table();
    table.ColumnWidths = "120 120 120";
    page.Paragraphs.Add(table);
    Row r1 = table.Rows.Add();
    Cell c1 = r1.Cells.Add();
    Cell c2 = r1.Cells.Add();
    Cell c3 = r1.Cells.Add();
    
    RadioButtonField rf = new RadioButtonField(page);
    rf.PartialName = "radio";
    doc.Form.Add(rf, 1);
    
    RadioButtonOptionField opt1 = new RadioButtonOptionField();
    RadioButtonOptionField opt2 = new RadioButtonOptionField();
    RadioButtonOptionField opt3 = new RadioButtonOptionField();
    
    opt1.OptionName = "Item1";
    opt2.OptionName = "Item2";
    opt3.OptionName = "Item3";
    
    opt1.Width = 15;
    opt1.Height = 15;
    opt2.Width = 15;
    opt2.Height = 15;
    opt3.Width = 15;
    opt3.Height = 15;
    
    rf.Add(opt1);
    rf.Add(opt2);
    rf.Add(opt3);
    
    opt1.Border = new Border(opt1);
    opt1.Border.Width = 1;
    opt1.Border.Style = BorderStyle.Solid;
    opt1.Characteristics.Border = System.Drawing.Color.Black;
    opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
    opt1.Caption = new TextFragment("Item1");
    opt2.Border = new Border(opt1);
    opt2.Border.Width = 1;
    opt2.Border.Style = BorderStyle.Solid;
    opt2.Characteristics.Border = System.Drawing.Color.Black;
    opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
    opt2.Caption = new TextFragment("Item2");
    opt3.Border = new Border(opt1);
    opt3.Border.Width = 1;
    opt3.Border.Style = BorderStyle.Solid;
    opt3.Characteristics.Border = System.Drawing.Color.Black;
    opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
    opt3.Caption = new TextFragment("Item3");
    c1.Paragraphs.Add(opt1);
    c2.Paragraphs.Add(opt2);
    c3.Paragraphs.Add(opt3);
    
    dataDir = dataDir + "RadioButtonWithOptions_out.pdf";
    // Save the PDF file
    doc.Save(dataDir);
    為RadioButtonField添加標(biāo)題

    以下代碼段顯示了如何添加與RadioButtonField關(guān)聯(lián)的標(biāo)題:

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    // Load source PDF form
    Aspose.Pdf.Facades.Form form1 = new Aspose.Pdf.Facades.Form(dataDir + "RadioButtonField.pdf");
    Document PDF_Template_PDF_HTML = new Document(dataDir + "RadioButtonField.pdf");
    foreach (var item in form1.FieldNames)
    {
        Console.WriteLine(item.ToString());
        Dictionary radioOptions = form1.GetButtonOptionValues(item);
        if (item.Contains("radio1"))
        {
            Aspose.Pdf.Forms.RadioButtonField field0 = PDF_Template_PDF_HTML.Form[item] as Aspose.Pdf.Forms.RadioButtonField;
            Aspose.Pdf.Forms.RadioButtonOptionField fieldoption = new Aspose.Pdf.Forms.RadioButtonOptionField();
            fieldoption.OptionName = "Yes";
            fieldoption.PartialName = "Yesname";
    
            var updatedFragment = new Aspose.Pdf.Text.TextFragment("test123");
            updatedFragment.TextState.Font = FontRepository.FindFont("Arial");
            updatedFragment.TextState.FontSize = 10;
            updatedFragment.TextState.LineSpacing = 6.32f;
    
            // Create TextParagraph object
            TextParagraph par = new TextParagraph();
    
            // Set paragraph position
            par.Position = new Position(field0.Rect.LLX, field0.Rect.LLY + updatedFragment.TextState.FontSize);
            // Specify word wraping mode
            par.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
    
            // Add new TextFragment to paragraph
            par.AppendLine(updatedFragment);
    
            // Add the TextParagraph using TextBuilder
            TextBuilder textBuilder = new TextBuilder(PDF_Template_PDF_HTML.Pages[1]);
            textBuilder.AppendParagraph(par);
    
            field0.DeleteOption("item1");
        }
    }
    PDF_Template_PDF_HTML.Save(dataDir + "RadioButtonField_out.pdf");
    添加組合框字段

    以下代碼段顯示了如何在PDF文檔中添加ComboBox字段。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    // Create Document object
    Document doc = new Document();
    
    // Add page to document object
    doc.Pages.Add();
    
    // Instantiate ComboBox Field object
    ComboBoxField combo = new ComboBoxField(doc.Pages[1], new Aspose.Pdf.Rectangle(100, 600, 150, 616));
    
    // Add option to ComboBox
    combo.AddOption("Red");
    combo.AddOption("Yellow");
    combo.AddOption("Green");
    combo.AddOption("Blue");
    
    // Add combo box object to form fields collection of document object
    doc.Form.Add(combo);
    dataDir = dataDir + "ComboBox_out.pdf";
    // Save the PDF document
    doc.Save(dataDir);

    修改PDF文檔中的表單字段

    要修改現(xiàn)有的表單字段,您可以從Form集合中獲取該字段并設(shè)置其屬性。然后保存更新的PDF文檔。以下代碼段顯示了如何修改PDF文檔中的現(xiàn)有表單字段。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    // Open document
    Document pdfDocument = new Document(dataDir + "ModifyFormField.pdf");
    
    // Get a field
    TextBoxField textBoxField = pdfDocument.Form["textbox1"] as TextBoxField;
    
    // Modify field value
    textBoxField.Value = "New Value";
    textBoxField.ReadOnly = true;
    dataDir = dataDir + "ModifyFormField_out.pdf";
    // Save updated document
    pdfDocument.Save(dataDir);

    從PDF文檔中刪除特殊表格字段

    所有表單字段都包含在Document對(duì)象的Form集合中。該集合提供了管理表單字段的不同方法,包括該Delete方法。如果要?jiǎng)h除特定字段,請(qǐng)將字段名稱(chēng)作為參數(shù)傳遞給Delete方法,然后保存更新的PDF文檔。以下代碼段顯示了如何從PDF文檔中刪除特定字段。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
    
    // Open document
    Document pdfDocument = new Document(dataDir + "DeleteFormField.pdf");
    
    // Delete a particular field by name
    pdfDocument.Form.Delete("textbox1");
    dataDir = dataDir + "DeleteFormField_out.pdf";
    // Save modified document
    pdfDocument.Save(dataDir);

    還想要更多嗎?您可以點(diǎn)擊閱讀
    【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢
    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();