Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Add Simple/Underline Text or Border Around Text in a PDF File Using Java  RSS feed

sherazam khan
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This technical tip shows how to Java Developers can add text in an existing PDF file using Aspose.Pdf for Java. To add text to an existing PDF file Open the input PDF using the Document object and get the particular page to which you want to add the text. After that create a TextFragment object with the input text along with other text properties. The TextBuilder object created from that particular page – to which you want to add the text – allow you to add the TextFragment object to the page using the AppendText method. Now just call the Document object's Save method and save the output PDF file.

The following code snippet shows you how to add text in an existing PDF file.


//open document

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

//get particular page

com.aspose.pdf.Page pdfPage = pdfDocument.getPages().get_Item(1);

//create text fragment

com.aspose.pdf.TextFragment textFragment = new com.aspose.pdf.TextFragment("main text");

textFragment.setPosition(new com.aspose.pdf.Position(100, 600));

//set text properties





// create TextBuilder object

com.aspose.pdf.TextBuilder textBuilder = new com.aspose.pdf.TextBuilder(pdfPage);

// append the text fragment to the PDF page

<bt>// save updated PDF file"Text_Added.pdf");

Overview: Aspose.Pdf for Java

Aspose.Pdf is a Java PDF component to create PDF documents without using Adobe Acrobat. It supports Floating box, PDF form field, PDF attachments, security, Foot note & end note, Multiple columns document, Table of Contents, List of Tables, Nested tables, Rich text format, images, hyperlinks, JavaScript, annotation, bookmarks, headers, footers and many more. Now you can create PDF by API, XML and XSL-FO files. It also enables you to converting HTML, XSL-FO and Excel files into PDF.

More about Aspose.Pdf for Java
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!