翻譯|使用教程|編輯:李顯亮|2021-06-21 10:20:01.800|閱讀 317 次
概述:PDF 表格可用于獲取調查數據或作為錄取表格。鑒于此,本文將教您 如何使用 C++ 創建、填寫和編輯可填寫的 PDF 表單。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
隨著計算機和互聯網的出現,許多信息都以數字方式捕獲。不同的公司想出了解決方案來提高這個過程的效率。一種這樣的解決方案是可填寫的 PDF 表單。PDF 表單是一種流行的選擇,可以輕松地以數字方式捕獲信息。PDF 表格可用于獲取調查數據或作為錄取表格。鑒于此,本文將教您 如何使用 C++ 創建、填寫和編輯可填寫的 PDF 表單。
Aspose.PDF for C++是一個 C++ 庫,允許您創建、閱讀和更新 PDF 文檔。此外,該 API 支持創建、填寫和編輯可填寫的 PDF 表單。點擊下方按鈕可下載試用。
在這個例子中,我們將從頭開始創建一個帶有兩個文本框和一個單選按鈕的表單。但是,一個文本框是多行的,另一個是單行的。以下是在 PDF 文件中創建表單的步驟。
以下示例代碼顯示了如何使用 C++ 在 PDF 文件中創建表單。
// Create an instance of the Document class auto pdfDocument = MakeObject(); // Add a blank page to the document System::SharedPtrpage = pdfDocument->get_Pages()->Add(); System::SharedPtrrectangle1 = MakeObject(275, 740, 440, 770); // Create a TextBoxField System::SharedPtrnameBox = MakeObject(pdfDocument, rectangle1); nameBox->set_PartialName(u"nameBox1"); nameBox->get_DefaultAppearance()->set_FontSize(10); nameBox->set_Multiline(true); System::SharedPtrnameBorder = MakeObject(nameBox); nameBorder->set_Width(1); nameBox->set_Border(nameBorder); nameBox->get_Characteristics()->set_Border(System::Drawing::Color::get_Black()); nameBox->set_Color(Aspose::Pdf::Color::FromRgb(System::Drawing::Color::get_Red())); System::SharedPtrrectangle2 = MakeObject(275, 718, 440, 738); // Create a TextBoxField System::SharedPtrmrnBox = MakeObject(pdfDocument, rectangle2); mrnBox->set_PartialName(u"Box1"); mrnBox->get_DefaultAppearance()->set_FontSize(10); System::SharedPtrmrnBorder = MakeObject(mrnBox); mrnBox->set_Width(165); mrnBox->set_Border(mrnBorder); mrnBox->get_Characteristics()->set_Border(System::Drawing::Color::get_Black()); mrnBox->set_Color(Aspose::Pdf::Color::FromRgb(System::Drawing::Color::get_Red())); // Add TextBoxField to the form pdfDocument->get_Form()->Add(nameBox, 1); pdfDocument->get_Form()->Add(mrnBox, 1); // Create a table System::SharedPtrtable = MakeObject
示例代碼生成的 PDF 文件的圖像
在這個例子中,我們將使用上一個例子中生成的文件。我們將使用Document 類加載文件并填充其字段。以下是填寫現有 PDF 表單字段的步驟。
以下示例代碼顯示了如何使用 C++ 填充 PDF 文件中的現有表單。
// Load the PDF file auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Fillable_PDF_Form.pdf"); // Retrieve the text box fields System::SharedPtr<TextBoxField> textBoxField1 = System::DynamicCast<TextBoxField>(pdfDocument->get_Form()->idx_get(u"nameBox1")); System::SharedPtr<TextBoxField> textBoxField2 = System::DynamicCast<TextBoxField>(pdfDocument->get_Form()->idx_get(u"Box1")); // Set the value of the text box fields textBoxField1->set_Value(u"A quick brown fox jumped over the lazy dog."); textBoxField2->set_Value(u"A quick brown fox jumped over the lazy dog."); // Retrieve the radio button field System::SharedPtr<RadioButtonField> radioField = System::DynamicCast<RadioButtonField>(pdfDocument->get_Form()->idx_get(u"radio")); // Set the value of the radio button field radioField->set_Selected(1); // Save the output file pdfDocument->Save(u"OutputDirectory\\Fill_PDF_Form_Field_Out.pdf");
示例代碼生成的 PDF 文件的圖像
使用 Aspose.PDF for C++,我們還可以修改之前填寫的字段的值。在這個例子中,我們將使用上一個例子中生成的文件并修改第一個TextBoxField的值。為此,請按照以下步驟操作。
以下示例代碼顯示了如何使用 C++ 修改 PDF 表單中字段的值。
// Load the PDF file auto pdfDocument = MakeObject(u"SourceDirectory\\Fill_PDF_Form_Field.pdf"); // Retrieve the TextBoxField System::SharedPtrtextBoxField = System::DynamicCast(pdfDocument->get_Form()->idx_get(u"nameBox1")); // Update the value of the TextBoxField textBoxField->set_Value(u"Changed Value"); // Mark the TextBoxField as readonly textBoxField->set_ReadOnly(true); // Save the output file pdfDocument->Save(u"OutputDirectory\\Modify_Form_Field_out.pdf");
示例代碼生成的 PDF 文件的圖像
API 還允許您從現有 PDF 表單中刪除表單域。以下是從 PDF 表單中刪除表單域的步驟。
以下示例代碼顯示了如何使用 C++ 從現有 PDF 表單中刪除表單域。
// Load the PDF file auto pdfDocument = MakeObject(u"SourceDirectory\\Fill_PDF_Form_Field.pdf"); // Delete the field pdfDocument->get_Form()->Delete(u"nameBox1"); // Save the output file pdfDocument->Save(u"OutputDirectory\\Delete_Form_Field_out.pdf");
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn