iText 5.4.5 を使用して、いくつかの入力 PDF ドキュメントをマージするタスクに取り組んでいます。入力ドキュメントには AcroForms が含まれている場合と含まれていない場合があり、フォームもマージしたいと考えています。
ここにあるサンプルの pdf ファイルを使用しています。これがコード例です。
public class TestForms {
@Test
public void testNoForms() throws DocumentException, IOException {
test("pdf/hello.pdf", "pdf/hello_memory.pdf");
}
@Test
public void testForms() throws DocumentException, IOException {
test("pdf/subscribe.pdf", "pdf/filled_form_1.pdf");
}
private void test(String first, String second) throws DocumentException, IOException {
OutputStream out = new FileOutputStream("/tmp/out.pdf");
InputStream stream = getClass().getClassLoader().getResourceAsStream(first);
PdfReader reader = new PdfReader(new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(stream)), null);
InputStream stream2 = getClass().getClassLoader().getResourceAsStream(second);
PdfReader reader2 = new PdfReader(new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(stream2)), null);
Document pdfDocument = new Document(reader.getPageSizeWithRotation(1));
PdfCopy pdfCopy = new PdfCopy(pdfDocument, out);
pdfCopy.setFullCompression();
pdfCopy.setCompressionLevel(PdfStream.BEST_COMPRESSION);
pdfCopy.setMergeFields();
pdfDocument.open();
pdfCopy.addDocument(reader);
pdfCopy.addDocument(reader2);
pdfCopy.close();
reader.close();
reader2.close();
}
}
- フォームを含む入力ファイルを使用する
NullPointerException
と、圧縮を有効または無効にできます。 - 標準入力ドキュメントでは、出力ファイルが作成されますが、Acrobat で開くと、問題があり (14) と表示され、コンテンツが表示されません。
- 標準入力ドキュメントと圧縮を無効にすると、出力が作成され、Acrobat に表示されます。
- 以前は を使用してこれを行い
PdfCopyFields
ましたが、 のブール値フラグを支持して非推奨になりましmergeFields
たPdfCopy
。これは正しいですか? そのフラグには javadoc がなく、それに関するドキュメントが見つかりませんでした。 - 前の質問に対する答えが「はい」であると仮定すると、コードに何か問題がありますか? ありがとう