bcooney Posted May 18, 2011 Posted May 18, 2011 I have a working SM function for stamping Page X of Y in my footer, but I'd like to suppress the page number on the first page. Any help appreciated.
brian rich Posted May 19, 2011 Posted May 19, 2011 I have a working SM function for stamping Page X of Y in my footer, but I'd like to suppress the page number on the first page. Any help appreciated. Put the result of the function as a merge field in the footer. Apply conditional formatting to the merge field so that when the function get(pagenumber) = 1 is true, the text is formatted as white. Should work OK unless your paper is coloured or your PDFs have a coloured background. HTH Brian Put the result of the function as a merge field in the footer. Apply conditional formatting to the merge field so that when the function get(pagenumber) = 1 is true, the text is formatted as white. Should work OK unless your paper is coloured or your PDFs have a coloured background. HTH Brian Sorry, didn't see that this referred to iText Brian
bcooney Posted May 19, 2011 Author Posted May 19, 2011 Thanks anyway, Brian. Yes, this is taking a several existing PDFs (not created in FM), merging them and stamping page numbers on all but the first page. However, the first page needs to count in the logic page numbers. That is, the second page needs to say 2 of n. My dilemma, is that if split the PDF, stamp the second half, and then merge the first page back on, I don't have the correct number of pages for the second half. Also, I don't know how to start page numbering at 2. I've searched iText forums, and there are no posts that address this, which is surprising, as I would think this is a common requirement.
brian rich Posted May 19, 2011 Posted May 19, 2011 Thanks anyway, Brian. Yes, this is taking a several existing PDFs (not created in FM), merging them and stamping page numbers on all but the first page. However, the first page needs to count in the logic page numbers. That is, the second page needs to say 2 of n. My dilemma, is that if split the PDF, stamp the second half, and then merge the first page back on, I don't have the correct number of pages for the second half. Also, I don't know how to start page numbering at 2. I've searched iText forums, and there are no posts that address this, which is surprising, as I would think this is a common requirement. Could you produce a temporary copy of the document un-numbered, and split off the first page so this remains unnumbered. Now on the original document stamp all the pages, then split page one off again. Then merge the temp doc first page with the split original document and that is your final document with the first page unnumbered. Brian
john renfrew Posted May 19, 2011 Posted May 19, 2011 This snippet will do what you want If as suggested on the iText lists you haven't yet bought and read the book it is the Fount Of All Knowledge... // snippet of code to miss out first page header // takes fm_file and stamps it to fm_fileOut import com.itextpdf.text.* import com.itextpdf.text.pdf.* import com.itextpdf.text.Font.* /** * Create a header table with page X of Y * @param x the page number * @param y the total number of pages * @return a table that can be used as header */ public static PdfPTable getHeaderTable(int x, int y) { PdfPTable table = new PdfPTable(2) table.setTotalWidth(527) table.setLockedWidth(true) table.getDefaultCell().setFixedHeight(20) table.getDefaultCell().setBorder(Rectangle.BOTTOM) table.addCell("This is the header zone") table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT) table.addCell(String.format("Page %d of %d", x, y)) return table } // Create a reader reader = new PdfReader(fm_file) // Create a stamper stamper = new PdfStamper(reader, new FileOutputStream(fm_fileOut)) // Loop over the pages and add a header to each page int n = reader.getNumberOfPages() for (i = 1; i <= n; i++) { if ( i==1 ) { } else { getHeaderTable(i, n).writeSelectedRows( 0, -1, 34, 803, stamper.getOverContent(i)) // this is the position of the header on the document } } // Close the stamper stamper.close() return true attached my example files... John doc1.pdf doc2.pdf
john renfrew Posted May 19, 2011 Posted May 19, 2011 Slight improvement Takes a number for which page you want to start Takes a 1 if it is a header otherwise it is a footer... Doesn't check for any errors etc etc but otherwise good // XofY ( fm_file ; fm_fileOut ; startPage ; header ) // 11_05_19 JR // v1 // snippet of code to miss out n page header/footer // takes fm_file and stamps it to fm_fileOut // startPage is the page to start the numbering // if header is 1 then it is a header otherwise it is a footer import com.itextpdf.text.* import com.itextpdf.text.pdf.* /** * Create a header table with page X of Y * @param x the page number * @param y the total number of pages * @param ctext is the text to appear in first cell * @return a table that can be used as header */ public getHeaderTable(int x, int y) { table = new PdfPTable(2) // this value is total width of table table.setTotalWidth(527) table.setLockedWidth(true) table.getDefaultCell().setFixedHeight(20) table.getDefaultCell().setBorder(Rectangle.BOTTOM) table.addCell(ctext) table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT) table.addCell(String.format("Page %d of %d", x, y)) return table } pos = 0 // pos is vertical position from bottom if(header=='1'){ mt = 'header ' pos = 803 }else{ mt ='footer ' pos = 43} ctext = 'This is the ' + mt + 'zone' // Create a reader reader = new PdfReader(fm_file) // Create a stamper stamper = new PdfStamper(reader, new FileOutputStream(fm_fileOut)) // Loop over the pages and add a header to each page int n = reader.getNumberOfPages() for (i = 1; i <= n; i++) { if ( i < startPage.toInteger() ) { } else { getHeaderTable(i, n).writeSelectedRows( 0, -1, 34, pos, stamper.getOverContent(i)) } } // Close the stamper stamper.close() return true
bcooney Posted May 19, 2011 Author Posted May 19, 2011 Both of you are so wonderful! Thank you so much. I'll put this into play tomorrow and let you know how it goes.
bcooney Posted March 30, 2012 Author Posted March 30, 2012 ...a year goes by Hey John, are you around? I cannot get the XofY to work. If you're around and can put this in an fm file, that would be great.
john renfrew Posted March 31, 2012 Posted March 31, 2012 Hmm, not the cleanest of code.... This works here and shows the basic parts. No error checks or stuff like that Needs full system paths obviously, but now makes the header width 90% of the first page width // XofY ( fm_file ; fm_fileOut ; startPage ; header ) // 12_04_01 JR // v1.5 // snippet of code to miss out n page header/footer // takes fm_file and stamps it to fm_fileOut // startPage is the page to start the numbering // if header is 1 then it is a header otherwise it is a footer import com.itextpdf.text.* import com.itextpdf.text.pdf.* /** * Create a header table with page X of Y * @param x the page number * @param y the total number of pages * @param ctext is the text to appear in first cell * @return a table that can be used as header */ // Closure to create header table getHeaderTable = { x, y -> table = new PdfPTable(2) // this value is total width of table table.setTotalWidth(tableW) table.setLockedWidth(true) table.getDefaultCell().setFixedHeight(24) // if header draw line at bottom else at top if (header=='1'){ table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM) table.getDefaultCell().setPaddingBottom(3) table.getDefaultCell().setBorder(Rectangle.BOTTOM) } else { table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP) table.getDefaultCell().setPaddingTop(2) table.getDefaultCell().setUseAscender(true) table.getDefaultCell().setBorder(Rectangle.TOP) } //end if table.addCell(new Phrase (cText, font2)) table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT) table.getDefaultCell().setPaddingTop(3) table.getDefaultCell().setUseAscender(true) table.addCell(new Phrase (String.format("Page %d of %d", x, y), font1)) return table } pos = 0 // pos is vertical position from bottom if(header=='1'){ mt = 'header ' pos = 803 }else{ mt ='footer ' pos = 43} //replace this with text of choice ctext = 'This is the ' + mt + 'zone' font1 = FontFactory.getFont(FontFactory.HELVETICA, 9) // Create a reader reader = new PdfReader(fm_file) pS = reader.getPageSizeWithRotation(1) wide = pS.getWidth() * 0.9 tableW = wide.toInteger() // Create a stamper stamper = new PdfStamper(reader, new FileOutputStream(fm_fileOut)) // Loop over the pages and add a header to each page n = reader.getNumberOfPages() for (i in 1..n ) { if ( i < startPage.toInteger() ) { } else { getHeaderTable(i, n).writeSelectedRows( 0, -1, 34, pos, stamper.getOverContent(i)) } } // Close the stamper stamper.close() return true
Recommended Posts
This topic is 4688 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now