Free Spire.PDF for Java, a free third-party library to manipulate PDF documents in Java, can not only support adding header and footer when creating a new PDF document, but also accomplish the requirement of adding multiple headers in a single PDF document. This tutorial will demonstrate how to do those in Java applications.
First of all, you need to add Spire.Pdf.jar in the library to IDEA. You can download the package from the link and manually add it, or reference it by adding the following configurations in your project’s pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Add Header and Footer to a PDF document
Free Spire.PDF for Java has a class named PdfPageTemplateElement, which represents a page template element that can be used as header, footer, watermark or stamp. The template can contain text, image as well as dynamic fields like PdfPageCountField,
PdfPageNumberField, etc. We use text string for the header and dynamic fields for the footer
in the following example.
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 AddHeaderFooter {
public static void main(String[] args) {
//create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set margins
PdfMargins margin = new PdfMargins(60,60,40,40);
//Call the method addHeaderAndFooter() to add header and footer
addHeaderAndFooter(doc, PdfPageSize.A4, margin);
//Add two pages to the PDF document and draw string to it.
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 for Java";
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);
//Draw 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 dynamic fields as 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(), - margin.getLeft(), y);
}
}Output
Add multiple headers to a PDF documentThe multiple headers on PDF means that different page has different header on the same PDF
document.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class MultipleHeaders {
public static void main(String[] args) {
//load the sample PDF document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
String header1 = "Spire.PDF for Java";
String header2 = "Add Multiple headers";
//define style
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD,12));
PdfBrush brush= PdfBrushes.getBlue();
Rectangle2D rect = new Rectangle2D.Float();
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(doc.getPageSettings().getSize().getWidth(),50f);
rect.setFrame(new Point2D.Float(0, 20), dimension2D);
PdfStringFormat format=new PdfStringFormat();
format.setAlignment(PdfTextAlignment.Center);
//draw header string for the first page
doc.getPages().get(0).getCanvas().drawString(header1,font,brush,rect,format);
//draw header string for the second page
format.setAlignment( PdfTextAlignment.Left);
doc.getPages().get(1).getCanvas().drawString(header2, font, brush, rect, format);
//save the document
doc.saveToFile("output/addDifferentHeaders.pdf", FileFormat.PDF);
}
}Output
No comments:
Post a Comment