翻譯|使用教程|編輯:李顯亮|2021-06-21 10:20:01.800|閱讀 317 次
概述:PDF 表格可用于獲取調(diào)查數(shù)據(jù)或作為錄取表格。鑒于此,本文將教您 如何使用 C++ 創(chuàng)建、填寫和編輯可填寫的 PDF 表單。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
隨著計(jì)算機(jī)和互聯(lián)網(wǎng)的出現(xiàn),許多信息都以數(shù)字方式捕獲。不同的公司想出了解決方案來提高這個(gè)過程的效率。一種這樣的解決方案是可填寫的 PDF 表單。PDF 表單是一種流行的選擇,可以輕松地以數(shù)字方式捕獲信息。PDF 表格可用于獲取調(diào)查數(shù)據(jù)或作為錄取表格。鑒于此,本文將教您 如何使用 C++ 創(chuàng)建、填寫和編輯可填寫的 PDF 表單。
Aspose.PDF for C++是一個(gè) C++ 庫(kù),允許您創(chuàng)建、閱讀和更新 PDF 文檔。此外,該 API 支持創(chuàng)建、填寫和編輯可填寫的 PDF 表單。點(diǎn)擊下方按鈕可下載試用。
點(diǎn)擊下載最新版Aspose.PDF for C++
在這個(gè)例子中,我們將從頭開始創(chuàng)建一個(gè)帶有兩個(gè)文本框和一個(gè)單選按鈕的表單。但是,一個(gè)文本框是多行的,另一個(gè)是單行的。以下是在 PDF 文件中創(chuàng)建表單的步驟。
以下示例代碼顯示了如何使用 C++ 在 PDF 文件中創(chuàng)建表單。
// 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 文件的圖像
在這個(gè)例子中,我們將使用上一個(gè)例子中生成的文件。我們將使用Document 類加載文件并填充其字段。以下是填寫現(xiàn)有 PDF 表單字段的步驟。
以下示例代碼顯示了如何使用 C++ 填充 PDF 文件中的現(xiàn)有表單。
// 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++,我們還可以修改之前填寫的字段的值。在這個(gè)例子中,我們將使用上一個(gè)例子中生成的文件并修改第一個(gè)TextBoxField的值。為此,請(qǐng)按照以下步驟操作。
以下示例代碼顯示了如何使用 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 還允許您從現(xiàn)有 PDF 表單中刪除表單域。以下是從 PDF 表單中刪除表單域的步驟。
以下示例代碼顯示了如何使用 C++ 從現(xiàn)有 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的全部完整功能,可聯(lián)系在線客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn