Posts

Showing posts with the label append text

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 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.clo...

Appending hyperlink into Docx file using docx4j library

Download docx4j library. filepath = valid path to .docx file url = hyperlink to append into end of the file public boolean appendUrlDocx() { try { File inputFile = new File(filepath); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputFile); // Create hyperlink P.Hyperlink link = createHyperlink(wordMLPackage, url); // Add it to a paragraph org.docx4j.wml.ObjectFactory wmlFactory = new org.docx4j.wml.ObjectFactory(); org.docx4j.wml.P paragraph = wmlFactory.createP(); paragraph.getParagraphContent().add(link); wordMLPackage.getMainDocumentPart().addObject(paragraph); // Now save it wordMLPackage.save(inputFile); return true; } catch (InvalidFormatException ex) { ex.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTr...