Save PDF Documents
- 4 minutes to read
Use the PdfDocument.save() method to write a PDF document to an output stream or a writable channel. You can also specify SaveOptions for the save operation.
Save a Document to a Writable Channel
Use the save(WritableByteChannel channel, SaveOptions options) method to save a PDF document to a writable channel and specify save options.
The following code snippet loads a PDF document, adds a page, and saves the document to a writable channel:
import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.printing.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Add an A4-size page to the document.
pdfDocument.getPages().add(DXPaperKind.A4);
// Configure save options.
SaveOptions saveOptions = new SaveOptions();
saveOptions.setUpdateCreatedAt(true);
saveOptions.setSyncMetadata(true);
// 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, saveOptions);
}
}
}
}
Save a Document to an Output Stream
Use the save(OutputStream stream) method to save a PDF document to an output stream.
The following code snippet loads a PDF document, adds a page, and saves the document to an output stream:
import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.printing.*;
import java.io.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream stream =
Files.newInputStream(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(stream)) {
// Add an A4-size page to the document.
pdfDocument.getPages().add(DXPaperKind.A4);
// Save the document to a PDF file.
try (OutputStream outputStream =
Files.newOutputStream(Path.of("Result.pdf"))) {
pdfDocument.save(outputStream);
}
}
}
}
Specify the PDF File Version
Call the PdfDocument.setFileVersion(value) method to specify the PDF version that the API writes to the file header when it saves the document. The following code snippet saves a document as a PDF 2.0 file:
import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
Files.createDirectories(Path.of("output"));
try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(inputStream)) {
pdfDocument.setFileVersion(FileVersion.PDF_2_0);
try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
pdfDocument.save(outputStream);
}
}
}
}
Resolve Form Field Name Conflicts
If multiple form fields in a document have the same full name, the API can rename the fields or throw an exception when it saves the document. Call the SaveOptions.setFieldNameConflictResolution(value) method to specify the conflict resolution mode:
| Value | Description |
|---|---|
| RENAME_NEW_CONFLICTS | The API renames form fields to resolve new name conflicts. Conflicts that the loaded document contains remain. |
| THROW_ON_NEW_CONFLICTS | The API throws an exception if the document contains new form field name conflicts. Conflicts that the loaded document contains do not cause an exception. |
| RENAME_ALL_CONFLICTS | The API renames form fields to resolve all name conflicts, including conflicts that the loaded document contains. |
| THROW_ON_ALL_CONFLICTS | The API throws an exception if the document contains form field name conflicts, including conflicts that the loaded document contains. |
If you do not specify the mode, the API renames form fields to resolve new name conflicts. A new conflict is a conflict that the document did not contain when you loaded it. The API keeps the name of the first field in the conflict and assigns names such as Field0 and Field1 to other fields.
PdfDocument.save methods return a SaveResult object. Call the SaveResult.getResolvedFieldNameConflicts() method to get renamed fields. To find conflicts before you save the document, call the PdfDocument.getFieldNameCollisions() method.
The following code snippet adds two form fields with the same name to a document, saves the document, and displays names of the kept and renamed fields:
import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument document = new PdfDocument()) {
Page page = document.getPages().add(595, 842);
// Add two form fields with the same name.
TextBoxField firstField = new TextBoxField("Name");
TextBoxField secondField = new TextBoxField("Name");
document.getFields().add(firstField);
document.getFields().add(secondField);
page.getAnnotations().add(new TextBoxWidgetAnnotation(firstField, new RectangleF(50, 700, 150, 20)));
page.getAnnotations().add(new TextBoxWidgetAnnotation(secondField, new RectangleF(50, 670, 150, 20)));
SaveOptions options = new SaveOptions();
options.setFieldNameConflictResolution(FieldNameConflictResolution.RENAME_NEW_CONFLICTS);
Files.createDirectories(Path.of("output"));
try (OutputStream stream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
SaveResult result = document.save(stream, options);
for (FormFieldNameConflict conflict : result.getResolvedFieldNameConflicts())
System.out.println(conflict.getTargetField().getName() + " -> " + conflict.getRenamedField().getName());
}
}
}
}