--
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/>*
------------------------------------------------------------------------------
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