Forget the underdocument and overdocument, get the page dimensions from the PdfReader, preferably from the cropbox. Write the header and footer to the overcontent.
Paulo ________________________________ From: Ivan Monnier [mailto:[email protected]] Sent: Monday, February 28, 2011 10:47 AM To: [email protected] Subject: [iText-questions] Some headers/footers -- Hello, Below is a method to add headers and footers to an existing document. It works fine on most document but sometimes, on some pages, especially if they seem to have been scanned, the header/footer is not displayed. It seems to be here because a selection on something invisible can be performed. There must be a foreground/background issue there but I was unable to find any clue to make sure the my header/footer are guaranteed to appear. Any clue ? import com.lowagie.text.*; import com.lowagie.text.pdf.*; /** * Add header + footer */ public static boolean addHeadFoot( String ficpdf_orig, String ficpdf_dest, String sheader, String sfooter) { int n; Document underdocument = null; Document overdocument = null; //PdfCopy writer = null; PdfStamper pdfstamper = null; PdfReader reader; File filefrom, fileto; PdfContentByte undercontent ; PdfContentByte overcontent ; PdfPTable pdffooter=null; PdfPTable pdfheader = null; float fret; try { // we create a reader for a certain document reader = new PdfReader(ficpdf_orig); reader.consolidateNamedDestinations(); // we retrieve the total number of pages n = reader.getNumberOfPages(); // step 1: creation of a document-object pdfstamper = new PdfStamper(reader, new FileOutputStream(ficpdf_dest)); // step 3: we open the document if (sheader.length() > 0) { pdfheader = new PdfPTable(1); pdfheader.setTotalWidth(600); pdfheader.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); pdfheader.getDefaultCell().setBorderColor(new Color(255, 255, 255)); pdfheader.addCell(new Phrase(sheader, new Font(Font.HELVETICA, 8, Font.ITALIC, new Color(0, 0, 0)))); } if (sfooter.length() > 0) { pdffooter = new PdfPTable(1); pdffooter.setTotalWidth(550); pdffooter.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); pdffooter.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM); pdffooter.getDefaultCell().setBorderColor(new Color(255, 255, 255)); pdffooter.addCell(new Phrase(sfooter, new Font(Font.HELVETICA, 8, Font.ITALIC, new Color(0, 0, 0)) )); } // step 4: we add content for (int i = 0 ; i < n ; ) { ++i; undercontent = pdfstamper.getUnderContent(i); overcontent = pdfstamper.getOverContent(i); underdocument = undercontent.getPdfDocument(); overdocument = overcontent.getPdfDocument(); if (sheader.length() > 0) { fret = pdfheader.writeSelectedRows(0, -1, 0, overdocument.top() , overcontent); } if (sfooter.length() > 0) { fret = pdffooter.writeSelectedRows(0, -1, 0, underdocument.bottom(), undercontent); } } // step 5: we close the document pdfstamper.close(); reader.close(); } catch(Exception e) { return false; } return true; } Regards. <http://www.qwamci.com/> Aviso Legal: Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem. Disclaimer: This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.
------------------------------------------------------------------------------ Free Software Download: Index, Search & Analyze Logs and other IT data in Real-Time with Splunk. Collect, index and harness all the fast moving IT data generated by your applications, servers and devices whether physical, virtual or in the cloud. Deliver compliance at lower cost and gain new business insights. http://p.sf.net/sfu/splunk-dev2dev
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions iText® is a registered trademark of 1T3XT BVBA. 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
