Create PDF Documents
- 2 minutes to read
Use the PdfDocument class to create a new document, add pages, place text, images, shapes, and save the result as a PDF file.
Create a PDF Document
Note
The document does not contain any pages when you create it. Add one or more pages before you add content or save the document.
The following code snippet creates a PDF document and adds a Letter-size page:
import com.devexpress.docs.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
// Create a new PDF document.
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add a Letter-size page to the document.
pdfDocument.getPages().add(DXPaperKind.LETTER);
// Save the document to a PDF file.
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}
Refer to the following help topic for additional information on page operations: Manage PDF Pages.
Add Content to Pages
A Page object stores its content as a collection of fragments (text blocks, images, shapes, or forms).
Create a fragment and add it to the page’s fragments collection. Use the Page.getFragments() method to access the fragments collection:
page.getFragments().add(fragment);
Use the following methods to add specific fragment types:
| Method | Description |
|---|---|
| addTextFragment() | Adds a text fragment. |
| addImageFragment() | Adds an image fragment. |
| addFragment() | Adds any page fragment, including PathFragment and FormFragment. |
PDF Coordinate System
Specify the position and size of each fragment when you add content to a page.
The DevExpress PDF Document API uses the following coordinate system:
| Parameter | Value |
|---|---|
| Origin | Bottom-left corner of the page |
| Units | Points (1 inch = 72 points) |
| Rotation | Degrees (positive values indicate clockwise rotation) |