PDF header or footer is a good place to display extra information about the document, for instance, you can insert a logo, contact information and page numbers to header or footer so that your document looks more professional and informative. In this article, I’ll show you how to add images, text, lines and page numbers to an exising PDF document at the header and footer spaces by using Free Spire.PDF for Java.
Spire.PDF没有提供直接将页眉或页脚应用于PDF文档的方法。 为此,我创建了drawHeader方法和drawFooter方法。 以下代码段适用于从常规Word文档转换而来的大多数PDF文档,其中页边距为“正常”(顶部/底部72pt,右侧/左侧90pt)。 如果您的PDF文档的页边距不同,请尝试修改X和Y的值以获得正确的结果。
Using the code
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
public class InsertHeaderAndFooter {
public static void main(String[] args) {
//Load a PDF document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.pdf");
//Draw header on the document
drawHeader(doc);
//Draw footer on the document
drawFooter(doc);
//Save the document
doc.saveToFile("output/InsertHeaderAndFooter.pdf");
}
public static void drawHeader(PdfDocument doc)
{
//Get page size
Dimension2D pageSize = doc.getPages().get(0).getSize();
//Declare two float variable
float x = 90;
float y = 20;
for (int i = 0; i < doc.getPages().getCount(); i++)
{
//Draw image at the specified position
PdfImage headerImage = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\your-logo-here.jpg");
float width = headerImage.getWidth()/3;
float height = headerImage.getHeight()/3;
doc.getPages().get(i).getCanvas().drawImage(headerImage, x, y, width, height);
//Draw a line under the image
PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
doc.getPages().get(i).getCanvas().drawLine(pen, x, y + height + 2, pageSize.getWidth() - x, y + height + 2);
}
}
public static void drawFooter(PdfDocument doc)
{
//Get page size
Dimension2D pageSize = doc.getPages().get(0).getSize();
//Declare two float variable
float x = 90;
float y = (float) pageSize.getHeight()- 72;
for (int i = 0; i < doc.getPages().getCount(); i++)
{
//Draw a line at the specified position
PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
doc.getPages().get(i).getCanvas().drawLine(pen, x, y, pageSize.getWidth() - x, y);
//Draw text at the specified position
y = y + 5;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String footerText = "Your Name Here\nTel:0101112222\nWebsite:www.yourwebsite.com";
doc.getPages().get(i).getCanvas().drawString(footerText, font, PdfBrushes.getBlack(), x, y, format);
//Draw page number
PdfPageNumberField number = new PdfPageNumberField();
PdfPageCountField count = new PdfPageCountField();
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0} of {1}", number, count);
compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));
Dimension2D fontSize = font.measureString(compositeField.getText());
compositeField.setBounds(new Rectangle2D.Float((float) (pageSize.getWidth() - x - fontSize.getWidth()), y, (float)fontSize.getWidth(), (float)fontSize.getHeight()));
compositeField.draw(doc.getPages().get(i).getCanvas());
}
}
}