iText - add content to existing PDF file

前端 未结 4 864
温柔的废话
温柔的废话 2020-11-27 13:09

I want to do the following with iText:

(1) parse an existing PDF file

(2) add some data to it, on the existing single page of the document (such as a timesta

4条回答
  •  死守一世寂寞
    2020-11-27 13:34

    iText has more than one way of doing this. The PdfStamper class is one option. But I find the easiest method is to create a new PDF document then import individual pages from the existing document into the new PDF.

    // Create output PDF
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    
    // Load existing PDF
    PdfReader reader = new PdfReader(templateInputStream);
    PdfImportedPage page = writer.getImportedPage(reader, 1); 
    
    // Copy first page of existing PDF into output PDF
    document.newPage();
    cb.addTemplate(page, 0, 0);
    
    // Add your new data / text here
    // for example...
    document.add(new Paragraph("my timestamp")); 
    
    document.close();
    

    This will read in a PDF from templateInputStream and write it out to outputStream. These might be file streams or memory streams or whatever suits your application.

提交回复
热议问题