0

In our application, we store PDF in the server and users download it. I need to add the header and footer to the PDF when it is retrieved from the server as byte array and we are using ITEXT API for it.

Here is the code snippet

ByteArrayOutputStream baos = new ByteArrayOutputStream(baNotes.length);
baos.write(baNotes, 0, baNotes.length);
pdfReader = new PdfReader(baNotes);
pdfStamper = new PdfStamper(pdfReader,baos);
Font font = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD);
int noteSize = pdfReader.getNumberOfPages();
logger.debug("Retrieving the total number of pages: "+ noteSize);

for(int i=1; i<= noteSize; i++) {
   logger.debug("Adding footer to each notes: "+i );
   PdfContentByte content = pdfStamper.getUnderContent(i);
   Phrase pHeader = new Phrase(fHeader, font);
   Phrase pFooter = new Phrase(fMessage+i, font);
   ColumnText.showTextAligned(content, Element.ALIGN_LEFT,pHeader,60,770, 0);
   ColumnText.showTextAligned(content, Element.ALIGN_LEFT,pFooter,155,20, 0);
}
pdfStamper.close();
bOutput = baos.toByteArray();

If i use the above snippet, in few of the PDF the header and footer is overlapping with the existing content since the co-ordinates are fixed.

I tried many examples to avoid overlapping but in all the examples they are creating the PDF, but in my scenario the PDF is already created and stored in the server. I have also given the link for the ITEXT resource section below for similar question. I also could not able to find from which co-ordinates the actual PDF content is starting.

why does my header overlap my content

Any suggestion/ideas on how to move forward or code snippets are very much appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deepak
  • 103
  • 1
  • 9
  • 1
    [This answer](https://stackoverflow.com/a/26503289/1729265) deals with finding free space on a pdf page using itext 5. You can use the topmost found free rectangle to put your header into etc.. – mkl Jan 23 '19 at 22:03
  • @mkl Thanks for the inputs and really appreciate it. I implemented your model code and tried but in most scenarios there is no rectangle space at the top of the PDF. So this approach didn't satisfy the application needs. I noticed that all the PDF has same page size. So do you think I can increase the page size of the PDF and insert the header at top? – Deepak Jan 24 '19 at 16:58
  • *"do you think I can increase the page size of the PDF and insert the header at top?"* - as far as pdf files are concerned, this indeed is an option. Beware, there may already be off-page material in the pdf, so you should paint a white rectangle in the new area before drawing text. I obviously cannot tell whether such a change in page format will *satisfy the application needs*... – mkl Jan 24 '19 at 21:39
  • Thanks for you help @mkl I followed this approach [link](https://stackoverflow.com/questions/29775893/how-to-extend-the-page-size-of-a-pdf-to-add-a-watermark) and increased the margin space with one stamper and added text on top of it with another stamper object. – Deepak Feb 06 '19 at 15:26

0 Answers0