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.
Call createCompare(comparedDocument) on the base document, then use doCompare() for each page pair. Both documents in the example are open and contain at least one page.
import com.compdfkit.core.compare.CPDFCompare;
import com.compdfkit.core.compare.CPDFCompareResults;
import com.compdfkit.core.compare.CPDFCompareResult;
CPDFCompare compare = baseDocument.createCompare(comparedDocument);
if (compare != null) {
try {
CPDFCompareResults results = compare.doCompare(
0, 0, CPDFCompare.CompType.ONLY_TEXT, false);
if (results != null) {
for (CPDFCompareResult difference : results.getTextResults()) {
// difference.getResultTypeEnum(): the difference type.
// difference.oldRect and compRect: positions in each side's PDF page coordinates.
}
int inserted = results.getInsertCount();
int deleted = results.getDeleteCount();
int replaced = results.getReplaceCount();
} else {
// Comparison failed; do not interpret this as no differences.
}
} finally {
compare.close();
}
}The caller still manages the source documents after closing the comparison object. Do not close either source document during comparison.
CompType | Content |
|---|---|
ONLY_TEXT | Text |
ONLY_IMAGE | Images |
ONLY_ANNOT | Annotations |
ONLY_PATH | Vector paths |
ONLY_SHADING | Shadings |
COMPARE_ALL | Text, images, and annotations; excludes paths and shadings |
Combine types with bitwise OR. Use CompType.everything() to include every type:
int targets = CPDFCompare.CompType.ONLY_TEXT.getId()
| CPDFCompare.CompType.ONLY_IMAGE.getId();
CPDFCompareResults results = compare.doCompare(0, 0, targets, false);The type mask cannot be 0. Use valid page indexes for each document. The application determines which pages correspond across documents.
Read differences with getTextResults(), getImageResults(), getAnnotResults(), getPathResults(), and getShadingResults(). isEmpty() means no differences were found for the requested types.
Each item includes its type, base page index oldPageIndex, compared page index compPageIndex, and rectangles for both sides. Rectangles use PDF page coordinates with the origin at the bottom left. Inserted or deleted content may lack a valid rectangle on one side; check before displaying it.
Use the reported getInsertCount(), getDeleteCount(), and other counts rather than list sizes. A replacement can produce entries for both sides. The comparison object's getTotal*Count() methods return accumulated session counts.
Set drawHighlight to true, then call generateNewDoc():
CPDFCompare compare = baseDocument.createCompare(comparedDocument);
if (compare != null) {
try {
compare.setInsertResColor(android.graphics.Color.BLUE);
compare.setDeleteResColor(android.graphics.Color.RED);
CPDFCompareResults results = compare.doCompare(
0, 0, CPDFCompare.CompType.everything(), true);
if (results != null) {
boolean saved = compare.generateNewDoc(outputPath);
// Handle the output according to saved.
}
} finally {
compare.close();
}
}Create the output directory and use a separate output path. drawHighlight = true writes annotations into the source pages in memory and marks both documents as modified, including on failure or when no differences are found. Open separate document instances for comparison if you need to preserve the active editing session.