Sol
Sol

Reputation: 889

Duplicate a page on Document using iTextSharp

I'm creating a PDF using iTextSharp. This PDF will have n-pages where all of the pages are exactly the same, except for a small text change. So I wanted to clone my Document object, do the change and add as a new page to the document, is it possible?

EDIT: To clarify a little what I have to do. I'm creating a document, and right now is on the memory as an iTextSharp.Document object. The user will save this to his computer (it is an web application) but in this case, the file that he will save should consiste of 3 pages, every page with the same content, except for one Paragraph on the bottom that changes.

What I did now was recreating the tables\paragraphs etc on each page, of the document, what I wanted was get the Document I created representing one page, clone it 3 times, and join as pages on a document.

Upvotes: 0

Views: 3106

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

This is a possible solution. It may not be the solution you want, but... that's not our fault as you are making us guess regarding your intentions.

Suppose that you have a PDF document with a single page: state.pdf

You want to add some text to this page, so that it looks like this: california.pdf

You don't want to do this once, but you want to do this multiple times, so that you get this: united_states_2.pdf

In that case, you need a combination of PdfStamper (to add the content to the single page) and PdfSmartCopy (to combine the different pages without bloating the document).

This is shown in the FillFlattenMerge2 example:

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    Document document = new Document();
    PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
    document.open();
    ByteArrayOutputStream baos;
    PdfReader reader;
    PdfStamper stamper;
    AcroFields fields;
    StringTokenizer tokenizer;
    BufferedReader br = new BufferedReader(new FileReader(DATA));
    String line = br.readLine();
    while ((line = br.readLine()) != null) {
        // create a PDF in memory
        baos = new ByteArrayOutputStream();
        reader = new PdfReader(SRC);
        stamper = new PdfStamper(reader, baos);
        fields = stamper.getAcroFields();
        tokenizer = new StringTokenizer(line, ";");
        fields.setField("name", tokenizer.nextToken());
        fields.setField("abbr", tokenizer.nextToken());
        fields.setField("capital", tokenizer.nextToken());
        fields.setField("city", tokenizer.nextToken());
        fields.setField("population", tokenizer.nextToken());
        fields.setField("surface", tokenizer.nextToken());
        fields.setField("timezone1", tokenizer.nextToken());
        fields.setField("timezone2", tokenizer.nextToken());
        fields.setField("dst", tokenizer.nextToken());
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        // add the PDF to PdfCopy
        reader = new PdfReader(baos.toByteArray());
        copy.addDocument(reader);
        reader.close();
    }
    br.close();
    document.close();
}

If this is not what you want, please clarify your question. In any case: this answer is one of the many possible correct interpretations of your question.

Upvotes: 1

Related Questions