Search code examples
openpdf

Header not showing in PDF document


I am trying to add a header to each page in my document. I am using OpenPDF 1.3.29 installed through Maven.

Here is a test program:

package test;

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;

public class HeaderTest {
  public static void main(String[] args) 
  throws Exception {
    Document doc = null;
    PdfWriter writer = null;
    try {
      doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
      writer = PdfWriter.getInstance(doc, new FileOutputStream("C:\\Tmp\\test.pdf"));
      doc.open();
      
      Font headerFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD,18);
      HeaderFooter header = new HeaderFooter(new Phrase("Test Header",headerFont), false);
      doc.setHeader(header);
      
      doc.add(new Paragraph("Test Content"));

    } finally {
      try { doc.close(); } catch( Exception e ) { }
      try { writer.close(); } catch( Exception e ) { }
    }
  }
}

The resulting PDF contains the content paragraph, but not the header.

Looking at the sample code, this seems like it should work.

Any idea what I did wrong?


Solution

  • I figured it out.

    I needed to set the header and footer before calling open() on the document. Also, I changed the header and footer to use Paragraph instead of Phrase. This is strange because the JavaDocs use Phrase.

    Anyway, this code works as expected:

    package test;
    
    import java.io.FileOutputStream;
    
    import com.lowagie.text.Document;
    import com.lowagie.text.Element;
    import com.lowagie.text.Font;
    import com.lowagie.text.FontFactory;
    import com.lowagie.text.HeaderFooter;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.Phrase;
    import com.lowagie.text.Rectangle;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.pdf.PdfWriter;
    
    public class HeaderTest {
      public static void main(String[] args) 
      throws Exception {
        Document doc = null;
        PdfWriter writer = null;
        try {
          doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
          writer = PdfWriter.getInstance(doc, new FileOutputStream("C:\\Tmp\\test.pdf"));
          
          HeaderFooter header = new HeaderFooter(new Paragraph("Test Header"), false);
          header.setAlignment(HeaderFooter.ALIGN_CENTER);
          header.setBorder(Rectangle.NO_BORDER);
          doc.setHeader(header);
          
          HeaderFooter footer = new HeaderFooter(new Paragraph("This is page: "), true);
          footer.setBorder(Rectangle.NO_BORDER);
          footer.setAlignment(Element.ALIGN_RIGHT);
          doc.setFooter(footer);
          
          doc.open();
    
          doc.add(new Paragraph("Test Content"));
    
        } finally {
          try { doc.close(); } catch( Exception e ) { }
          try { writer.close(); } catch( Exception e ) { }
        }
      }
    }