gpt4 book ai didi

java - PdfCopy 中的 iText mergeFields 创建无效的 pdf

转载 作者:行者123 更新时间:2023-12-01 13:37:06 24 4
gpt4 key购买 nike

我正在研究使用 iText 5.4.5 合并一些输入 PDF 文档的任务。输入文档可能包含也可能不包含 AcroForms,我也想合并表单。

我正在使用找到的示例 pdf 文件 here这是代码示例:

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 执行此操作,但现在已弃用它,转而使用 PdfCopy 中的 boolean 标志 mergeFields,这是正确的吗?该标志上没有 javadoc,而且我找不到有关它的文档。
  • 假设上一个问题的答案是"is",我的代码有什么问题吗?谢谢

最佳答案

我们正在使用 PdfCopy 合并不同的文件,某些文件可能有字段。我们使用版本5.5.3.0。代码很简单,看起来工作正常,但有时结果文件无法打印!我们的代码:

Public Shared Function MergeFiles(ByVal sourceFiles As List(Of Byte())) As Byte()
Dim document As New Document()
Dim output As New MemoryStream()
Dim copy As iTextSharp.text.pdf.PdfCopy = Nothing
Dim readers As New List(Of iTextSharp.text.pdf.PdfReader)
Try
copy = New iTextSharp.text.pdf.PdfCopy(document, output)
copy.SetMergeFields()
document.Open()
For fileCounter As Integer = 0 To sourceFiles.Count - 1
Dim reader As New PdfReader(sourceFiles(fileCounter))
reader.MakeRemoteNamedDestinationsLocal()
readers.Add(reader)
copy.AddDocument(reader)
Next
Catch exception As Exception
Throw exception
Finally
If copy IsNot Nothing Then copy.Close()
document.Close()
For Each reader As PdfReader In readers
reader.Close()
Next
End Try
Return output.GetBuffer()
End Function

关于java - PdfCopy 中的 iText mergeFields 创建无效的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21189981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com