0
votes

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.

1
Maybe it's very easy, maybe it's very hard. It all depends on the nature of the small text change you want to apply. Can you elaborate? - Bruno Lowagie
One chunk of text will be changed on each page. But basically what I need more is to duplicate one page n times on the document. So for example, I create PAGE 1 and now I need PAGE 3 and 4 to be the same as PAGE 1. - Yohan Leafheart
What's keeping you from using PdfSmartCopy as indicated in my answer? - Bruno Lowagie
Doesn't PdfSmartCopy need a physical file to work? I do not have those, I'm doing everything on memory and sending to another funcion a MemoryStream which will be send to the response object - Yohan Leafheart
No, PdfSmartCopy needs a PdfReader instance. You can create such an instance using a byte[]. You can get a byte[] from a MemoryStream that holds a complete PDF. In the Java example in my answer, I have reader = new PdfReader(baos.toByteArray()); and this also exists in C#. - Bruno Lowagie

1 Answers

1
votes

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.