Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Android Guides Content comparison

Content Comparison ​

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.

Compare Pages ​

java
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.

Select Content Types ​

CompTypeContent
ONLY_TEXTText
ONLY_IMAGEImages
ONLY_ANNOTAnnotations
ONLY_PATHVector paths
ONLY_SHADINGShadings
COMPARE_ALLText, images, and annotations; excludes paths and shadings

Combine types with bitwise OR. Use CompType.everything() to include every type:

java
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 Results ​

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.

Export Highlighted Results ​

Set drawHighlight to true, then call generateNewDoc():

java
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.