我正在撰寫一個程式,它采用帶有一堆空白表單欄位的模板 PDF,制作它的副本,填寫表單,然后將欄位展平。
其中一個模板有大量欄位,因此撰寫一個填充所有欄位的方法會導致錯誤,指出由于方法的大小限制,代碼太大。
為了解決這個問題,我關閉了目標檔案,然后嘗試用另一種方法打開它,我可以在其中繼續填寫欄位,但這會導致一個錯誤,提示“(請求的操作不能對具有用戶的檔案執行-映射部分打開)”
我通過關閉 PDF 結束了第一種方法,所以我不確定問題是什么。程式將執行第一個方法,填充欄位,但在到達第二個方法時拋出錯誤。下面的示例代碼。
public void E2fill(String srcE2, String destE2) throws IOException
{
try
{
PdfDocument pdf2 = new PdfDocument(new PdfReader(destE2), new PdfWriter(dest2E2));
PdfAcroForm form2 = PdfAcroForm.getAcroForm(pdf2, true);
Map<String, PdfFormField> fields2 = form2.getFormFields();
PdfFormField field2;
fields2.get("fieldname1").setValue(stringname1);
//lots more field fills
pdf2.close()
}
catch(Exception x)
{
System.out.println(x.getMessage());
}
}
public void E2fill2(String destE2, String dest2E2) throws IOException
{
try
{
PdfDocument pdf2 = new PdfDocument(new PdfReader(destE2), new PdfWriter(dest2E2));
PdfAcroForm form2 = PdfAcroForm.getAcroForm(pdf2, true);
Map<String, PdfFormField> fields2 = form2.getFormFields();
PdfFormField field2;
fields2.get("fieldname546").setValue(stringname546);
//more field fills
form2.flattenFields();
pdf2.close();
}
catch(Exception x)
{
System.out.println(x.getMessage());
}
}
uj5u.com熱心網友回復:
我建議你試試這個:
public void fill(String srcE2, String destE2) {
PdfDocument pdf2 = new PdfDocument(new PdfReader(destE2), new PdfWriter(dest2E2));
PdfAcroForm form2 = PdfAcroForm.getAcroForm(pdf2, true);
Map<String, PdfFormField> fields2 = form2.getFormFields();
E2fill(fields2);
E2fill2(fields2);
form2.flattenFields();
pdf2.close();
}
public void E2fill(Map<String, PdfFormField> fields2) throws IOException
{
fields2.get("fieldname1").setValue(stringname1);
//lots more field fills
}
public void E2fill2(PdfAcroForm form2) throws IOException {
fields2.get("fieldname546").setValue(stringname546);
//more field fills
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/346236.html
