Ajouter un pied de page au PDF
Contents
[
Hide
]
Ajouter un pied de page au PDF
Utilisez PdfFileStamp lorsque vous avez besoin d’un contenu de pied de page répété sur chaque page d’un document.
Étapes
-
Créez une instance
PdfFileStampet liez le PDF source. -
Créez le contenu du pied de page sous forme de
FormattedTextou d’un flux d’image. -
Appelez la surcharge
addFooterappropriée. -
Enregistrez le fichier mis à jour et fermez l’objet de façade.
Exemples Java
public static void addTextFooter(Path inputFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
FormattedText text = new FormattedText("Sample Footer");
pdfStamper.addFooter(text, 20);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
public static void addImageFooter(Path inputFile, Path imageFile, Path outputFile) throws Exception {
PdfFileStamp pdfStamper = new PdfFileStamp();
try (InputStream imageStream = Files.newInputStream(imageFile)) {
pdfStamper.bindPdf(inputFile.toString());
pdfStamper.addFooter(imageStream, 20);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
public static void addFooterWithMargins(Path inputFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
FormattedText text = new FormattedText("This footer has margins on all sides.");
pdfStamper.addFooter(text, 20, 20, 20);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}