Add Header to PDF
Contents
[
Hide
]
Add header to PDF
Use PdfFileStamp when you need repeated header content on each page.
Steps
- Create a
PdfFileStampinstance and bind the source PDF. - Build the header content as
FormattedTextor load it from an image stream. - Call the appropriate
addHeaderoverload. - Save the output and close the facade object.
Java examples
public static void addTextHeader(Path inputFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
FormattedText text = new FormattedText("Sample Header");
pdfStamper.addHeader(text, 20);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
public static void addImageHeader(Path inputFile, Path imageFile, Path outputFile) throws Exception {
PdfFileStamp pdfStamper = new PdfFileStamp();
try (InputStream imageStream = Files.newInputStream(imageFile)) {
pdfStamper.bindPdf(inputFile.toString());
pdfStamper.addHeader(imageStream, 20);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
public static void addHeaderWithMargins(Path inputFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
FormattedText text = new FormattedText(
"Sample Header",
Color.BLUE,
FontStyle.Helvetica,
EncodingType.Winansi,
true,
12.0f);
pdfStamper.addHeader(text, 20, 20, 20);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}