Sidebar

How do you create a header and/or footer in a PDF created with itext?

0 votes
13.6K views
asked Nov 5, 2013 by oscar-p-6691 (550 points)
Using itext, how do you create a header and/or footer in a PDF file?
commented Nov 5, 2013 by oscar-p-6691 (550 points)
edited Nov 5, 2013 by oscar-p-6691
If anyone needs help with the margins or height please feel free to ask.  I have 3 headers and 2 footers in one of my PDF templates.  You can put as many as you like, just change the height of the cell (setFixedHeight) and the start of the bottom of the cell (tblheader.getTotalHeight() + 590).

I also set another cell to the right of the header to put the patient info in.  I just added another header called header2 and copied everything from header one and renamed it.  I then set parameter 3 of writeSelectedRows for header2 to 160 so everything was directly to the right of the first header with the labels in it.

2 Answers

+1 vote
 
Best answer

The easiest way I would fin is you will need to create a custom JAR file since javascript does not allow you to properly extend other classes.  I will give examples of the java file I used to create the JAR file as well as the javascript I used im my channel.  I created the java file in Eclipse and exported it to a JAR then imported it into Qvera.

 

Here is a snippet of the java code I used im my custom JAR file:

 

package com.itextpdf.header;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
 
public class HeaderFooter extends PdfPageEventHelper {

Paragraph header;
Paragraph footer;

/** The template with the total number of pages. */
PdfTemplate total;
 
Font font1 = new Font(FontFamily.TIMES_ROMAN, 11f);
Font font2 = new Font(FontFamily.TIMES_ROMAN, 11f, Font.BOLD);
Font font3 = new Font(FontFamily.TIMES_ROMAN, 10.5f);
Font font4 = new Font(FontFamily.TIMES_ROMAN, 10.5f, Font.BOLD);
Font font5 = new Font(FontFamily.TIMES_ROMAN, 7f);
Font font6 = new Font(FontFamily.TIMES_ROMAN, 7f, Font.BOLD);

/**
* Allows us to change the content of the header.
* @param header The new header String
*/
public void setHeader(Paragraph header) {
   this.header = header;
}

public void setFooter(Paragraph footer) {
   this.footer = footer;
}

/**
* Creates the PdfTemplate that will hold the total number of pages.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onOpenDocument(PdfWriter writer, Document document) {
    total = writer.getDirectContent().createTemplate(30, 16);
}

/**
* Adds a header to every page
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable tblheader = new PdfPTable(1);
PdfPTable tblfooter = new PdfPTable(1);
try {
tblheader.setWidths(new int[]{24});
tblheader.setTotalWidth(150);
tblheader.setLockedWidth(true);
tblheader.getDefaultCell().setFixedHeight(130);
tblheader.getDefaultCell().setLeading(0, 1.1f);
tblheader.getDefaultCell().setBorder(Rectangle.BOTTOM);
tblheader.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
tblheader.addCell(header1);
tblheader.writeSelectedRows(0, -1, 10, tblheader.getTotalHeight() + 590, writer.getDirectContent());

tblfooter.setWidths(new int[]{24});
tblfooter.setTotalWidth(590);
tblfooter.setLockedWidth(true);
tblfooter.getDefaultCell().setFixedHeight(40);
tblfooter.getDefaultCell().setLeading(0, 1.2f);
tblfooter.getDefaultCell().setBorder(Rectangle.TOP);
tblfooter.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
tblfooter.addCell(footer);
tblfooter.writeSelectedRows(0, -1, 10, tblfooter.getTotalHeight() + 70, writer.getDirectContent());

tblpages.setWidths(new int[]{103, 97});
tblpages.setTotalWidth(590);
tblpages.setLockedWidth(true);
tblpages.getDefaultCell().setFixedHeight(20);
tblpages.getDefaultCell().setBorder(Rectangle.TOP);
tblpages.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
Paragraph page = new Paragraph(String.format("Page %d of ", writer.getPageNumber()), font5);
tblpages.addCell(page);
PdfPCell pages = new PdfPCell(Image.getInstance(total));
pages.setBorder(Rectangle.TOP);
tblpages.addCell(pages).setHorizontalAlignment(Element.ALIGN_LEFT);
tblpages.writeSelectedRows(0, -1, 10, tblpages.getTotalHeight(), writer.getDirectContent());
}
   catch(DocumentException de) {
throw new ExceptionConverter(de);
}
}
 
/**
* Fills out the total number of pages before the document is closed.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(
*      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber() - 1), font5),
2, 7, 0);
}
}

 

answered Nov 5, 2013 by oscar-p-6691 (550 points)
edited Nov 5, 2013 by oscar-p-6691
+1 vote

Here is the javascript I used in my channel:

// This mapping uses the itextpdf-5.3.4.jar library found at
//   http://itextpdf.com

// copy the jar into the QIE\lib\ folder
// and then configure QIE to include the jar using the
//   'Manage External Libraries' button under 'System Configuration'

importPackage(java.io);

// get text out of all OBX-5 (uncommet this and comment out the above line for multiple OBX-5)
var obx5 = '';
var instances = message.getAllNodes('OBX-5');
for (var inst = 0 ; inst < instances.length ; inst++) {
   obx5 += instances[inst] + "~";
}

// parse into string array
var lines = obx5.split('~');
var event = new com.itextpdf.header.HeaderFooter();

// convert text to PDF
var byteArrayOutputStream = new java.io.ByteArrayOutputStream();
var document = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.LETTER, 12, 12, 220, 110);

// this fills the byteArrayOutputStream with the document, header and footer
var writer = com.itextpdf.text.pdf.PdfWriter.getInstance(document, byteArrayOutputStream);
writer.setPageEvent(event);

var font1 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 10.5);
var font2 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 10.5, com.itextpdf.text.Font.BOLD);
var font3 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 7);
var font4 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 7, com.itextpdf.text.Font.BOLD);

document.open();

var header = new com.itextpdf.text.Paragraph();
header.add(new com.itextpdf.text.Paragraph("Patient Name:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Date of Birth:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Service Date/Time:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Patient MRN:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Patient Location:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Hospital Study ID:\n", font2));
header.add(new com.itextpdf.text.Paragraph("PACS Study ID:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Ordering Physician:\n", font2));
header.add(new com.itextpdf.text.Paragraph("Procedure:", font2));

var footer = new com.itextpdf.text.Paragraph();
footer.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
footer.add(new com.itextpdf.text.Paragraph("\nFooter\n", font3));
footer.add(new com.itextpdf.text.Paragraph("Phone: (888) 555-7755\n", font3));
footer.add(new com.itextpdf.text.Paragraph("Support: (800) 555-1179", font3));

//document.add(paragraph);
event.setHeader1(header);
event.setFooter(footer);

for (var i = 0; i < lines.length; i++) {
   document.add(new com.itextpdf.text.Paragraph(lines[i]+"\n", font1));
}
document.close();

// create new message with PDF as the content
message = qie.createTextMessage();
message.setNode('/', byteArrayOutputStream.toString('ISO-8859-1'));

 

answered Nov 5, 2013 by oscar-p-6691 (550 points)
...