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.
ntimes 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 LeafheartPdfSmartCopyas indicated in my answer? - Bruno LowagiePdfSmartCopyneed 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 LeafheartPdfSmartCopyneeds aPdfReaderinstance. You can create such an instance using abyte[]. You can get abyte[]from aMemoryStreamthat holds a complete PDF. In the Java example in my answer, I havereader = new PdfReader(baos.toByteArray());and this also exists in C#. - Bruno Lowagie