importjava.io.File;importjava.io.IOException;importorg.apache.pdfbox.pdmodel.PDDocument;importorg.apache.pdfbox.pdmodel.PDPage;importorg.apache.pdfbox.pdmodel.PDPageContentStream;importorg.apache.pdfbox.pdmodel.font.PDType1Font;publicclassAddingTextToAPdf{publicstaticvoidmain(String args[])throwsIOException{//Loading an existing document PDDocument doc =PDDocument.load(newFile("C:/pdfBox/AddText_IP.pdf"));//Creating a PDF Document PDPage page = doc.getPage(0);PDPageContentStream contentStream =newPDPageContentStream(doc, page);//Begin the Content stream
contentStream.beginText();//Setting the font to the Content stream
contentStream.setFont(PDType1Font.TIMES_ROMAN,16);//Setting the position for the line
contentStream.newLineAtOffset(25,725);String text = "This is an example of adding text to a page in the pdf document.
we can add as many lines as we want like this using the draw string method
of the ContentStreamclass";//Adding text in the form of string
contentStream.showText(text);//Ending the content stream
contentStream.endText();System.out.println("Content added");//Closing the content stream
contentStream.close();//Saving the document
doc.save(newFile("C:/pdfBox/AddText_OP.pdf"));//Closing the document
doc.close();}}