Program PDF headers and footers in Java Free Spire.PDF for Java, a class library that allows you to create PDF documents directly from Java programs without installing any additional software. You can easily create PDF files using this library. Simply create a PDFDocument object, create as many PDFabageBase objects as you need, draw strings, images, tables, lists, headers, footers and other elements on the page and save the document. This statement adds headers and pages when creating a Spire.PDF for Java PDF document.
Java
import java.awt.*;
import java.awt.geom.Dimension2D;
import com.spire.pdf.*;
import com.spire.pdf.automaticfields.PdfAutomaticField;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;
public class PDFHeaderFooter {
public static void main(String[] args) throws Exception {
//Create a PDFDocument object
PdfDocument doc = new PdfDocument();
//Set margins
PdfMargins margin = new PdfMargins(60,60,40,40);
//Method addheaderandfooter()To add headers and footers.
addHeaderAndFooter(doc, PdfPageSize.A4, margin);
//Add one page to your PDF document and draw a string.
PdfPageBase page1 = doc.getPages().add();
PdfPageBase page2 = doc.getPages().add();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 14));
String text1 = "Demo of Spire.PDF";
String text2 = "How to add header and footer to PDF in JAVA";
page1.getCanvas().drawString(text1, font, PdfBrushes.getBlack(),0,0);
page2.getCanvas().drawString(text2, font, PdfBrushes.getBlack(),0,0);
//Save the document
doc.saveToFile("output/headerFooter.pdf");
doc.close();
}
static void addHeaderAndFooter(PdfDocument doc, Dimension2D pageSize, PdfMargins margin) {
PdfPageTemplateElement header = new PdfPageTemplateElement(margin.getLeft(), pageSize.getHeight());
doc.getTemplate().setLeft(header);
PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.getWidth(), margin.getTop());
topSpace.setForeground(true);
doc.getTemplate().setTop(topSpace);
//Drawing of header label
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String label = "E-iceblue Co.,Ltd";
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(font.measureString(label, format));
float y = topSpace.getHeight() - font.getHeight() - 1;
PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 0.75f);
topSpace.getGraphics().setTransparency(0.5f);
topSpace.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);
y = y - 1 - (float) dimension2D.getHeight();
topSpace.getGraphics().drawString(label, font, PdfBrushes.getBlack(), margin.getLeft(), y, format);
PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.getRight(), pageSize.getHeight());
doc.getTemplate().setRight(rightSpace);
//Draw a dynamic field as a footer
PdfPageTemplateElement footer = new PdfPageTemplateElement(pageSize.getWidth(), margin.getBottom());
footer.setForeground(true);
doc.getTemplate().setBottom(footer);
y = font.getHeight() + 1;
footer.getGraphics().setTransparency(0.5f);
footer.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);
y = y + 1;
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.setAutomaticFields(new PdfAutomaticField[]{pageNumber, pageCount});
pageNumberLabel.setBrush(PdfBrushes.getBlack());
pageNumberLabel.setFont(font);
format = new PdfStringFormat(PdfTextAlignment.Right);
pageNumberLabel.setStringFormat(format);
pageNumberLabel.setText("page {0} of {1}");
pageNumberLabel.setBounds(footer.getBounds());
pageNumberLabel.draw(footer.getGraphics(), pageSize.getWidth() - margin.getLeft(), y);
}
}
Recommended Posts