PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Use CPDFDocument.convertType() to convert an open PDF to a target standard type. Conversion adjusts document content or resources; success depends on the source and target. Check the return value and save the output.
| Type | PDFDocumentType values |
|---|---|
| PDF/A-1 | PDFTypeA1a, PDFTypeA1b |
| PDF/A-2 | PDFTypeA2a, PDFTypeA2u, PDFTypeA2b |
| PDF/A-3 | PDFTypeA3a, PDFTypeA3u, PDFTypeA3b |
| PDF/X | PDFTypeX1a, PDFTypeX3, PDFTypeX4 |
| PDF/E | PDFTypeE1, PDFTypeE2 |
| PDF/VT | PDFTypeVT1, PDFTypeVT2 |
| PDF/UA | PDFTypeUA1 |
The current enum does not include PDF/A-4. Do not infer support for types absent from the enum.
This example converts an open document to PDF/A-2b. outputPath is a writable new file path whose parent directory exists. Run the operation on a background thread.
CPDFDocument.PDFDocumentType target = CPDFDocument.PDFDocumentType.PDFTypeA2b;
if (document.convertType(target)) {
try {
boolean saved = document.saveAs(outputPath, false);
if (saved) {
CPDFDocument output = new CPDFDocument(context);
try {
if (output.open(outputPath)
== CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
boolean matches = output.getType() == target;
// Use matches to inspect the saved document type.
}
} finally {
output.close();
}
}
} catch (CPDFDocumentException e) {
// Conversion completed but saving failed. Retry or report the error as appropriate.
}
} else {
// Conversion failed; do not proceed as though it succeeded.
}val target = CPDFDocument.PDFDocumentType.PDFTypeA2b
if (document.convertType(target)) {
try {
if (document.saveAs(outputPath, false)) {
val output = CPDFDocument(context)
try {
if (output.open(outputPath) ==
CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
val matches = output.type == target
// Use matches to inspect the saved document type.
}
} finally {
output.close()
}
}
} catch (e: CPDFDocumentException) {
// Handle save failure.
}
} else {
// Handle conversion failure.
}The example uses CPDFDocument from com.compdfkit.core.document and CPDFDocumentException from com.compdfkit.core.common.
saveAs(path, true), then reopen the decrypted file before conversion.convertType() changes the document in memory. Save successful changes and close instances no longer needed.getType() checks the type recognized by the SDK and does not replace independent conformance validation. Validate separately when required for delivery.For other format conversions, see Conversion SDK.