Get Started — PDF Document API for Java
- 3 minutes to read
This tutorial uses the PDF Document API for Java to create a PDF document, add content, create annotations, and save the document.
Create a PDF Document
Note
Add the devexpress-docs-pdf dependency to use the DevExpress PDF Document API.
Refer to the following help topic for additional information: Add DevExpress API Libraries to a Project.
Use the PdfDocument class to create a new PDF document. Add pages to the document with the PdfDocument.getPages().add() method.
The following code snippet creates a PDF document with an A4 page:
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;
public class CreatePdfDocument {
public static void main(String[] args) throws Exception {
try (PdfDocument document = new PdfDocument()) {
Page page = document.getPages().add(DXPaperKind.A4);
}
}
}
Add Text Content
Use text fragments to add text to a PDF page. The following code snippet adds a title and a paragraph:
TextFragment title = new TextFragment();
title.setText("Quarterly Sales Report");
title.setLocation(new PointF(50, 770));
title.setFontSize(24);
page.addFragment(title);
ParagraphFragment paragraph = new ParagraphFragment();
paragraph.setText("This report summarizes sales data for Q1 2026.");
paragraph.setLocation(new PointF(50, 730));
paragraph.setWidth(300);
page.addFragment(paragraph);
Add Images
Use image fragments to add images to a PDF page.
byte[] imageData = Files.readAllBytes(Path.of("images/devexpress-logo.png"));
DXImage image = DXImage.fromStream(new ByteArrayInputStream(imageData));
page.addImageFragment(image, 100, 500);
You can add images from files, streams, or other image sources.
Add Annotations
Use annotation classes to add interactive elements and markup to PDF pages.
The following code snippet adds a text annotation:
TextAnnotation annotation = new TextAnnotation(new RectangleF(50, 500, 200, 100));
annotation.setTitle("Brian Zetc");
annotation.setContent("Review this section.");
page.getAnnotations().add(annotation);
The PDF Document API supports various annotation types, including:
- Text annotations
- Markup annotation
- Link annotations
- Drawing annotations
- Highlight annotations
- Stamp annotations
Refer to the following help topic for additional information: PDF Document Annotations.
Save a PDF Document
Use the PdfDocument.save() method to save a document as a PDF file.
try(WritableByteChannel channel =
FileChannel.open(
Path.of("Result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
document.save(channel);
}
The following image illustrates the result:

Tutorial Source Code
Expand this section to see the complete source code for the tutorial.
package pdf;
import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.DXImage;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;
import java.io.*;
import java.nio.file.*;
import java.nio.channels.*;
public class Main {
public static void main(String[] args) throws Exception {
byte[] imageData = Files.readAllBytes(Path.of("images/devexpress-logo.png"));
try (PdfDocument document = new PdfDocument();
DXImage image = DXImage.fromStream(new ByteArrayInputStream(imageData))) {
Page page = document.getPages().add(DXPaperKind.A4);
TextFragment title = new TextFragment();
title.setText("Quarterly Sales Report");
title.setLocation(new PointF(50, 770));
title.setFontSize(24);
page.addFragment(title);
ParagraphFragment paragraph = new ParagraphFragment();
paragraph.setText("This report summarizes sales data for Q1 2026.");
paragraph.setLocation(new PointF(50, 730));
paragraph.setWidth(300);
page.addFragment(paragraph);
page.addImageFragment(image, 500, 730);
TextAnnotation annotation = new TextAnnotation(new RectangleF(50, 500, 200, 100));
annotation.setTitle("Brian Zetc");
annotation.setContent("Review this section.");
page.getAnnotations().add(annotation);
try(WritableByteChannel channel =
FileChannel.open(
Path.of("Result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
document.save(channel);
}
}
}
}