Adding custom notes or watermarks often requires you to add text to PDF in Java applications. Conholdate.Total for Java is a powerful SDK that enables developers to edit PDF files programmatically. This guide walks you through the prerequisites, a detailed code walkthrough, and a complete example so you can overlay custom text on existing PDFs with ease.
Prerequisites and Setup
Before you start, make sure you have the following:
- Java 8 or higher installed.
- Maven for dependency management.
- An IDE such as IntelliJ IDEA or Eclipse.
- A valid Conholdate.Total for Java license for production use (see the licensing section later).
Add the Conholdate Maven repository and the SDK dependency to your pom.xml:
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
Download the latest SDK binaries from the download page. After the Maven build succeeds, you are ready to start coding. The next section shows exactly how to load a PDF and insert text.
Add Text to PDF in Java: Step-by-Step Walkthrough
Below is a concise walkthrough of each logical step. The full source code is reproduced later in the Complete Code Example section.
Step 1: Open the PDF and Create a TextBuilder
First, open the source PDF with the Document class, then create a TextBuilder pointing at the page you want to add text to.
String dataDir = "path/to/your/document/directory/";
// Open document
Document doc = new Document(dataDir + "Conholdate.pdf");
TextBuilder builder = new TextBuilder(doc.getPages().get_Item(1));
The Document and TextBuilder classes are documented in the API reference.
Step 2: Create and Configure a TextParagraph
Construct a TextParagraph and configure its layout: the indent applied to subsequent lines, the rectangle bounding the text area on the page, and the word-wrap mode.
// Create text paragraph
TextParagraph paragraph = new TextParagraph();
// Set subsequent lines indent
paragraph.setSubsequentLinesIndent(20);
// Specify the location to add TextParagraph
paragraph.setRectangle(new Rectangle(100, 300, 200, 700));
// Specify word wrapping mode
paragraph.getFormattingOptions().setWrapMode(TextFormattingOptions.WordWrapMode.ByWords);
Step 3: Create a TextFragment and Configure the Font
Create a TextFragment containing the text you want to overlay, then set its font and font size through getTextState().
// Create text fragment
TextFragment fragment1 = new TextFragment("the quick brown fox jumps over the lazy dog");
fragment1.getTextState().setFont(FontRepository.findFont("Times New Roman"));
fragment1.getTextState().setFontSize(12);
Step 4: Append the Fragment and the Paragraph
Add the text fragment as a line inside the paragraph, then append the paragraph to the page through the TextBuilder.
// Add fragment to paragraph
paragraph.appendLine(fragment1);
// Add paragraph
builder.appendParagraph(paragraph);
Step 5: Save the Modified PDF
Finally, build the output path and save the resulting document.
dataDir = dataDir + "AddText_out.pdf";
// Save resulting PDF document.
doc.save(dataDir);
With these steps, you have completed the PDF text insertion workflow.
Complete Code Example: Insert Text to PDF in Java
The following example demonstrates the entire process from loading the source file to saving the updated PDF.
This example demonstrates how to add text to an existing PDF using Conholdate.Total for Java.
import com.aspose.pdf.Document;
import com.aspose.pdf.TextBuilder;
import com.aspose.pdf.TextParagraph;
import com.aspose.pdf.Rectangle;
import com.aspose.pdf.TextFormattingOptions;
import com.aspose.pdf.TextFragment;
import com.aspose.pdf.FontRepository;
public class AddTextToPdf {
public static void main(String[] args) {
String dataDir = "path/to/your/document/directory/";
// Open document
Document doc = new Document(dataDir + "Conholdate.pdf");
TextBuilder builder = new TextBuilder(doc.getPages().get_Item(1));
// Create text paragraph
TextParagraph paragraph = new TextParagraph();
// Set subsequent lines indent
paragraph.setSubsequentLinesIndent(20);
// Specify the location to add TextParagraph
paragraph.setRectangle(new Rectangle(100, 300, 200, 700));
// Specify word wrapping mode
paragraph.getFormattingOptions().setWrapMode(TextFormattingOptions.WordWrapMode.ByWords);
// Create text fragment
TextFragment fragment1 = new TextFragment("the quick brown fox jumps over the lazy dog");
fragment1.getTextState().setFont(FontRepository.findFont("Times New Roman"));
fragment1.getTextState().setFontSize(12);
// Add fragment to paragraph
paragraph.appendLine(fragment1);
// Add paragraph
builder.appendParagraph(paragraph);
dataDir = dataDir + "AddText_out.pdf";
// Save resulting PDF document.
doc.save(dataDir);
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update
dataDirto point at your actual document directory (containingConholdate.pdf), verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Conclusion
In this tutorial we showed how to add text to PDF in Java using the robust features of Conholdate.Total for Java. By following the step‑by‑step walkthrough, you can reliably build text paragraphs with TextBuilder and TextParagraph, control font styling through TextFragment, and save the modified document. The SDK handles PDF page coordinates and text rendering internally, so you can focus on business logic. Remember to acquire a proper license for production use; you can obtain a temporary license from the temporary license page or explore pricing options on the pricing page. With these tools in hand, enhancing PDFs with custom text becomes a straightforward part of your Java application.
FAQs
How do I add text to PDF in Java without overwriting existing content?
Usebuilder.appendParagraph(paragraph)as shown in the example;TextBuilderappends the new paragraph to the target page, preserving any existing content.Is it possible to add a custom watermark text to PDF in Java?
Yes. The same approach works for watermarks — create aTextFragmentwith the desired watermark text, configure a suitable font and size (or transparency via the text state), and append it through aTextParagraphon each page.What should I do if the inserted text does not appear correctly for certain languages?
Choose a font that supports the required character set withFontRepository.findFont(). The SDK respects the font settings applied viagetTextState(), and the documentation provides guidance on handling different scripts.Can I batch process multiple PDFs to add the same text?
Absolutely. Wrap the code inside a loop that iterates over a list of file paths, reusing the sameDocument/TextBuilderlogic for each file.
