Right final version (hopefully!) :

public void createPdf(String filename) throws Exception {

    // Pass 1 : Build Content

    Document document = new Document(PageSize.A4);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);

    document.open();

    ...

    document.close();

    // Pass 2 : Header & Template

    // Header Page

    document = new Document();
PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(filename));

    document.open();

PdfReader reader = new PdfReader(new RandomAccessFileOrArray(HEADER), null);
    baos = new ByteArrayOutputStream();
    PdfStamper stamper = new PdfStamper(reader, baos);

    fillFields(stamper.getAcroFields(), 0);

    stamper.setFormFlattening(true);
    stamper.close();

copy.addPage(copy.getImportedPage(new PdfReader(baos.toByteArray()), 1));

    // Template

PdfReader contentReader = new PdfReader(new RandomAccessFileOrArray(baos.toByteArray()), null);

    int numPages = contentReader.getNumberOfPages();

    for (int page = 1; page <= numPages; page++)  {

        reader = new PdfReader(TEMPLATE);
        baos = new ByteArrayOutputStream();
        stamper = new PdfStamper(reader, baos);

        fillFields(stamper.getAcroFields(), page);

        stamper.setFormFlattening(true);

PdfImportedPage template = stamper.getImportedPage(contentReader, page);
        stamper.getOverContent(1).addTemplate(template, 0, 0);

        stamper.close();

        reader = new PdfReader(baos.toByteArray());
        copy.addPage(copy.getImportedPage(reader, 1));
    }

    document.close();
}

Le 15/02/2011 09:24, 1T3XT BVBA a écrit :
Op 14/02/2011 23:36, Antony Webster schreef:
OK but in this case in order to keep the same look as the original font field I'd need to retrieve its font family, weight and size. How can this be done? I've looked around online without much luck...
In that case, you should stick with your own design, and create the background in two passes: fill out the form, then use it as a background, BUT there's an important caveat: if you do it this way, adding a background as a PdfImportedPage that is slightly different for every page, your PDF will be bloated. The common part of the background (the form without the page numbers) will be added as many times as there are page. Therefore, you may want to use PdfSmartCopy instead of PdfStamper in your final pass. PdfSmartCopy will detect the redundant content, and you'll end up with PDFs that have a much smaller file size.


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb


_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to