-4

I am able to use java and itext to create a pdf file with a page header and with body content. You can view the resulting file at a file sharing site by clicking on this link.

How can I alter the code below so that a page footer is also created at the bottom of the page? I want the footer to say "MyCity, MyState, MyPostalCode MyPhoneNumber". I also want to footer to have a line above it just like the header has a line above it in the example link above.

Here is the code that generates the sample PDF in the link above:

public class HeaderFooter {

    public static final String RESULT = "C:\\path\\to\\headerfooter.pdf";

    public static void main(String[] args) throws SQLException, DocumentException, IOException {

        Document document = new Document(PageSize.A4, 36, 36, 54, 36);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph(""));
        document.add(Chunk.NEWLINE);
        document.add(new Paragraph("First paragraph."));
        document.add(new Paragraph("Second paragraph."));
        document.add(new Paragraph("Third paragraph."));
        document.newPage();
        document.close();

        PdfReader reader = new PdfReader(baos.toByteArray());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++) {
            getHeader(i, n).writeSelectedRows(0, -1, 34, 803, stamper.getOverContent(i));
        }
        stamper.close();
        reader.close();
    }

    public static PdfPTable getHeader(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 Document's Title.");
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        return table;
    }
}

For anyone who wants to take one minute to get the above code to work on their computer, the maven dependency to add to pom.xml is:

  <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.5</version>
  </dependency>
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • You know the meaning of the `writeSelectedRows` parameters, don't you? Thus, it should be fairly obvious... Hint: your `34, 803` are the coordinates where the header table is added. For a footer table use a fairly small y coordinate. – mkl Mar 31 '15 at 04:15

1 Answers1

-1

You have copy/pasted code from the TwoPasses example that I wrote for my book. Note that you should not copy/paste code written by someone else without some kind of attribution (such as a link to where you found the code). This is something I learned when I did the code review of iText (see the slides "Startup Legal and IP" I presented for the Founder Institute).

Also: when you copy/paste code that was written in the context of a book, please make sure that you act like a real developer and read what the code you are copy/pasting is supposed to do. For instance: in the book you took as inspiration, there is also the PdfCalendar example.

In this example, we also create a table that we add at an absolute position using the writeSelectedRows() method. Just like you, we defined a width using the setTotalWidth() method, which means that we can ask the table for its height using the getTotalHeight() method.

In the PdfCalendar example, we use this value as a parameter for the writeSelectedRows() method:

table.writeSelectedRows(0, -1, 169, table.getTotalHeight() + 20, canvas);

In this case, the x value is 169, a value that was obtained by taking the width of the page (A4 in landscape = 842) minus the width of the table (which we defined as 504), divided by two: 338 / 2 = 169.

As for y, we took the height of the table (which can vary depending on the number of days in the month) and we add 20 user units as a bottom margin.

You have chosen 34 as value for x, which is OK, but 806 for y, which is strange if you want the table to be a footer: 806 is usually at the top of the page (especially since you defined your page size as PageSize.A4).

If you are creating a document from scratch and you want to add a header and a footer, you may want to do this in one pass and add the footer using a page event.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Huh, a downvote? Isn't the solution I provided correct? – Bruno Lowagie Apr 01 '15 at 10:05
  • Hi @CodeMed please explain why the solution that was provided doesn't work for you. – Bruno Lowagie Apr 01 '15 at 10:06
  • @santiago I don't know, but on the other hand, the OP never accepted the answer, so I really want to know what is wrong with my answer. If it's incorrect, so be it. If it's correct, the OP can tell so by accepting the answer. – Bruno Lowagie Jul 06 '15 at 10:00
  • The OP has no obligation to accept any answer, correct or not. As downvotes are anonymous, it is impossible to know who did it unless they comment (which they do not have to). –  Jul 06 '15 at 10:02
  • @santiago Yeah, downvotes being anonymous is a pest. I lost 16 rep points in the last 24 hours, even on completely genuine questions such as http://stackoverflow.com/questions/12604171/data-encoding-when-submitting-a-pdf-form-using-acroform-technology Those are clearly hate-votes, not from people who understand the question, but from people who think they can hurt me by down voting. They don't understand how ridiculous that is. It probably gives them a sense of power. – Bruno Lowagie Jul 06 '15 at 10:06
  • 16 rep points shouldn't really harm you. May be the understand the question and find it is not all that good (in their view), maybe it is the 'meta effect', no way of knowing for sure. –  Jul 06 '15 at 10:08
  • It's the meta effect. I have brought http://stackoverflow.com/questions/12604171/data-encoding-when-submitting-a-pdf-form-using-acroform-technology to the ISO committee and the ISO committee agreed that it was a flaw in the PDF specification ISO-32000-1. It will be fixed in ISO-32000-2. Suppose that it would have been the other way round: many up-votes on SO and no response from the experts at the ISO committee, that would have been bad. Now the down-votes are nothing more than a testament of a flaw in SO. – Bruno Lowagie Jul 06 '15 at 10:10
  • I wouldn't sweat it, it all averages out –  Jul 06 '15 at 10:12
  • I know, but still: I have many different ways to spend my time. When I answer questions and I notice that my effort isn't appreciated, then I look for other, more rewarding ways to spend my time. Given the fact that I answered more than 1300 questions in less than 3 years time, many developers would be the victim of that, because there aren't that many people out there as knowledgeable about iText as its original creator. – Bruno Lowagie Jul 06 '15 at 10:14
  • at almost 30K - it has definitely averaged out in your favour. –  Jul 06 '15 at 10:22
  • Whats the meaning of this sentence? "In this case, the x value is" – waterlooalex Aug 09 '15 at 02:14