簡體   English   中英

PdfCopy中的iText mergeFields創建無效的pdf

[英]iText mergeFields in PdfCopy creates invalid pdf

我正在使用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但它現在贊成布爾標志的棄用mergeFieldsPdfCopy ,這是正確的? 該標志上沒有javadoc,我找不到有關它的文檔。
  • 假設上一個問題的答案是“是”,我的代碼有什么問題嗎? 謝謝

我們正在使用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

您對PdfCopy.setMergeFields()是正確的,並且合並代碼很好。

您描述的問題是由於爬入5.4.5的錯誤引起的。 它們應該在修訂版中修復。 6152和修復程序將包含在下一版本中。

謝謝讓我們注意到這個。

只是說我們有同樣的問題: PdfCopy中的iText mergeFields創建無效的pdf 因此在5.5.3.0版中仍未修復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM