OK I think I have it figured out (it works at any rate!). I'd be most 
interested in your opinions in terms of memory efficiency.


Many Thanks,


Anthony



public class Test {


    public static final String CONTENT = "content.pdf";
    public static final String HEADER = "header.pdf";
    public static final String TEMPLATE = "template.pdf";
    public static final String RESULT = "result.pdf";


    public static void main(String[] args) throws Exception {


        // First Pass - Create Content


        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, new FileOutputStream(CONTENT));
        document.open();


        Chapter chapter = new Chapter("Chapter", 1);
        chapter.setBookmarkOpen(true);
        chapter.add(new Paragraph("Chapter intro paragraph"));


        Section section = chapter.addSection("Section", 0);
        section.add(new Paragraph("Section paragraph"));
        section.add(new Paragraph("Section paragraph"));


        document.add(chapter);


        document.close();


        // Second Pass - Apply Template (optional)


        PdfReader contentReader = new PdfReader(new 
RandomAccessFileOrArray(CONTENT), null);
        PdfStamper contentStamper = new PdfStamper(contentReader, new 
FileOutputStream(RESULT));


        PdfStamper stamper;
        PdfReader reader = new PdfReader(new RandomAccessFileOrArray(TEMPLATE), 
null);


        int numPages = contentReader.getNumberOfPages();
        for (int i = 1; i <= numPages; i++) {


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


            AcroFields form = stamper.getAcroFields();


            Set<String> keys = new HashSet<String>(form.getFields().keySet());
            for (String key : keys) {
                form.setField(key, getStringValueFor(String reference));
            }
            stamper.setFormFlattening(true);


            stamper.close();


            PdfImportedPage page = contentStamper.getImportedPage(new 
PdfReader(baos.toByteArray()), 1);
            contentStamper.getUnderContent(i).addTemplate(page, 0, 0);
        }


        // Header


        ByteArrayOutputStream baos = new ByteArrayOutputStream();


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


        AcroFields form = stamper.getAcroFields();


        Set<String> keys = new HashSet<String>(form.getFields().keySet());
        for (String key : keys) {
            form.setField(key, getStringValueFor(String reference));
        }
        stamper.setFormFlattening(true);


        stamper.close();


        PdfImportedPage page = contentStamper.getImportedPage(new 
PdfReader(baos.toByteArray()), 1);


        contentStamper.insertPage(1, contentReader.getPageSize(1));
        contentStamper.getOverContent(1).addTemplate(page, 0, 0);


        contentStamper.close();
    }
}

  _____  

From: Antony Webster [mailto:[email protected]]
To: [email protected], Post all your questions about iText here 
[mailto:[email protected]]
Sent: Mon, 14 Feb 2011 00:19:18 +0100
Subject: Re: [iText-questions] PDF Watermarks

                OK this is as far as I've got so far. So far I'm only handling 
the front page (HEADER) however the HEADER pdf form fields don't seem to be 
filled.



// First Pass - Create Content


Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(CONTENT));


document.open();


Chapter chapter = new Chapter("Chapter", 1);
chapter.setBookmarkOpen(true);
chapter.add(new Paragraph("Chapter intro paragraph"));


Section section = chapter.addSection("Section", 0);
section.add(new Paragraph("Section paragraph"));
section.add(new Paragraph("Section paragraph"));


document.add(chapter);


document.close();


// Second Pass - Apply Header & Template (optional)


// Fill header fields


ByteArrayOutputStream baos = new ByteArrayOutputStream();


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


AcroFields form = stamper.getAcroFields();


Set<String> keys = new HashSet<String>(form.getFields().keySet());
for (String key : keys) {
    form.setField(key, getStringValueFor(String key));
}


stamper.close();


// 1.2 Insert into final document


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


stamper = new PdfStamper(contentReader, new FileOutputStream(RESULT));


PdfImportedPage page = stamper.getImportedPage(headerReader, 1);


stamper.insertPage(1, contentReader.getPageSize(1));
stamper.getOverContent(1).addTemplate(page, 0, 0);


stamper.close();

  _____  

From: Antony Webster [mailto:[email protected]]
To: [email protected]
Sent: Sun, 13 Feb 2011 21:09:42 +0100
Subject: [iText-questions] PDF Watermarks

                Hi,


I'm trying to merge various pdfs while both keeping memory use and final file 
size down. Here is my scenario.


I'm generating pdfs of approximately 90 pages. So far so good. However I'd like 
to merge each page of my 'content' pdf with a 'template' pdf. This 'template' 
pdf is like a 1 page watermark containing a header, a footer and possibly an 
actual watermark. Additionally the header and footer will contain fields which 
reference information about the context of the 'content' pdf (for instance 
'author', 'date', 'company' etc..). These fields will be filled in upon 
generation. They will stay the same for each page however the 'page number' 
field obviously won't. It doesn't stop there. I'd also like to append a 'title 
page' pdf to the final pdf file. Similarly this file with have fields which 
shall be filled in upon generation. 


Any help would be most appreciated...


Anthony    
------------------------------------------------------------------------------
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