Append hyperlink into PDF file using iText
download iText library
filepath = valid path to pdf file
url = hyperlink to show at the end of the pdf file
public boolean appendUrlPdf() {
try {
File inputFile = new File(filepath);
Document document = new Document(PageSize.A4);
PdfReader reader = new PdfReader(new FileInputStream(inputFile));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(inputFile));
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int i = 1; i < reader.getNumberOfPages() + 1; i++) {
PdfImportedPage page = writer.getImportedPage(reader, i);
Image instance = Image.getInstance(page);
document.add(instance);
}
Paragraph paragraph = new Paragraph();
Anchor anchor = new Anchor(url);
anchor.setReference(url);
paragraph.add(anchor);
document.add(paragraph);
document.close();
} catch (Exception ex) {
Logger.getLogger(UrlAppender.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
filepath = valid path to pdf file
url = hyperlink to show at the end of the pdf file
public boolean appendUrlPdf() {
try {
File inputFile = new File(filepath);
Document document = new Document(PageSize.A4);
PdfReader reader = new PdfReader(new FileInputStream(inputFile));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(inputFile));
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int i = 1; i < reader.getNumberOfPages() + 1; i++) {
PdfImportedPage page = writer.getImportedPage(reader, i);
Image instance = Image.getInstance(page);
document.add(instance);
}
Paragraph paragraph = new Paragraph();
Anchor anchor = new Anchor(url);
anchor.setReference(url);
paragraph.add(anchor);
document.add(paragraph);
document.close();
} catch (Exception ex) {
Logger.getLogger(UrlAppender.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
Comments
Post a Comment