Tutorial: How to Compare PDF Files with GroupDocs.Comparison Cloud
Learning Objectives
In this tutorial, you’ll learn how to:
- Compare two PDF documents to identify differences
- Work with PDF-specific comparison features
- Implement PDF comparison in different programming languages
- Interpret and handle comparison results for PDF files
Prerequisites
Before starting this tutorial, you’ll need:
- A GroupDocs.Comparison Cloud account (get a free trial here)
- Your Client ID and Client Secret credentials
- Source and target PDF files uploaded to your GroupDocs Cloud Storage
- Basic familiarity with RESTful APIs and your chosen programming language
Understanding PDF Comparison
PDF comparison is one of the most common document comparison scenarios. GroupDocs.Comparison Cloud API provides specialized functionality for comparing PDF documents, detecting changes in:
- Text content
- Images and graphics
- Form fields
- Annotations
- Document formatting and layout
Let’s learn how to implement PDF comparison with GroupDocs.Comparison Cloud.
Step 1: Upload PDF Documents (If Needed)
Before comparing PDF files, make sure you have uploaded both source and target PDFs to your cloud storage. For detailed instructions on file uploads, refer to the File API documentation.
Step 2: Understanding the API Resource
To compare PDF documents, we’ll use the same GroupDocs.Comparison Cloud REST API resource as for other file types:
POST https://api.groupdocs.cloud/v2.0/comparison/comparisons
This endpoint requires a JSON body with the source and target file paths, as well as the output path for the comparison result.
Step 3: Implementing with cURL
Let’s start with a cURL example specifically for PDF comparison:
# First, get the JWT token
curl -v "https://api.groupdocs.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
# Use the token to compare PDF documents
curl -v "https://api.groupdocs.cloud/v2.0/comparison/comparisons" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d "{
'SourceFile': {
'FilePath': 'source_files/pdf/source.pdf'
},
'TargetFiles': [
{
'FilePath': 'target_files/pdf/target.pdf'
}
],
'OutputPath': 'output/result.pdf'
}"
Replace YOUR_CLIENT_ID
, YOUR_CLIENT_SECRET
, and YOUR_JWT_TOKEN
with your actual credentials.
The response will provide information about the comparison result file:
{
"href": "https://api.groupdocs.cloud/v2.0/comparison/storage/file/output/result.pdf",
"rel": "output/result.pdf",
"type": "file",
"title": "result.pdf"
}
Step 4: Implementing with SDKs
Now let’s implement PDF comparison using various SDKs.
C# Example
using System;
using System.Collections.Generic;
using GroupDocs.Comparison.Cloud.Sdk.Api;
using GroupDocs.Comparison.Cloud.Sdk.Client;
using GroupDocs.Comparison.Cloud.Sdk.Model;
using GroupDocs.Comparison.Cloud.Sdk.Model.Requests;
namespace ComparePdfExample
{
class Program
{
static void Main(string[] args)
{
// Configure API credentials
var configuration = new Configuration("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
// Create CompareApi instance
var apiInstance = new CompareApi(configuration);
// Prepare comparison options
var options = new ComparisonOptions
{
SourceFile = new FileInfo { FilePath = "source_files/pdf/source.pdf" },
TargetFiles = new List<FileInfo> { new FileInfo { FilePath = "target_files/pdf/target.pdf" } },
OutputPath = "output/result.pdf"
};
// Create comparison request
var request = new ComparisonsRequest(options);
try
{
// Execute comparison
var response = apiInstance.Comparisons(request);
// Display result information
Console.WriteLine("PDF comparison completed successfully!");
Console.WriteLine($"Result file URL: {response.Href}");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}
Python Example
import groupdocs_comparison_cloud
from groupdocs_comparison_cloud import ComparisonOptions, FileInfo
# Configure API credentials
configuration = groupdocs_comparison_cloud.Configuration(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET"
)
# Create CompareApi instance
api_instance = groupdocs_comparison_cloud.CompareApi.from_keys(
configuration.client_id, configuration.client_secret
)
# Prepare comparison options
source = groupdocs_comparison_cloud.FileInfo()
source.file_path = "source_files/pdf/source.pdf"
target = groupdocs_comparison_cloud.FileInfo()
target.file_path = "target_files/pdf/target.pdf"
options = groupdocs_comparison_cloud.ComparisonOptions()
options.source_file = source
options.target_files = [target]
options.output_path = "output/result.pdf"
# Create comparison request
request = groupdocs_comparison_cloud.ComparisonsRequest(options)
try:
# Execute comparison
response = api_instance.comparisons(request)
# Display result information
print("PDF comparison completed successfully!")
print(f"Result file URL: {response.href}")
except groupdocs_comparison_cloud.ApiException as e:
print(f"Error: {e}")
Java Example
import com.groupdocs.cloud.comparison.client.*;
import com.groupdocs.cloud.comparison.model.*;
import com.groupdocs.cloud.comparison.api.CompareApi;
import com.groupdocs.cloud.comparison.model.requests.ComparisonsRequest;
import java.util.Collections;
public class ComparePdfExample {
public static void main(String[] args) {
// Configure API credentials
Configuration configuration = new Configuration("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
// Create CompareApi instance
CompareApi apiInstance = new CompareApi(configuration);
// Prepare comparison options
FileInfo sourceFileInfo = new FileInfo();
sourceFileInfo.setFilePath("source_files/pdf/source.pdf");
FileInfo targetFileInfo = new FileInfo();
targetFileInfo.setFilePath("target_files/pdf/target.pdf");
ComparisonOptions options = new ComparisonOptions();
options.setSourceFile(sourceFileInfo);
options.setTargetFiles(Collections.singletonList(targetFileInfo));
options.setOutputPath("output/result.pdf");
// Create comparison request
ComparisonsRequest request = new ComparisonsRequest(options);
try {
// Execute comparison
Link response = apiInstance.comparisons(request);
// Display result information
System.out.println("PDF comparison completed successfully!");
System.out.println("Result file URL: " + response.getHref());
} catch (ApiException e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
Node.js Example
// Load the GroupDocs.Comparison Cloud SDK
const comparison = require("groupdocs-comparison-cloud");
// Configure API credentials
const clientId = "YOUR_CLIENT_ID";
const clientSecret = "YOUR_CLIENT_SECRET";
// Create CompareApi instance
const compareApi = comparison.CompareApi.fromKeys(clientId, clientSecret);
// Prepare comparison options
const source = new comparison.FileInfo();
source.filePath = "source_files/pdf/source.pdf";
const target = new comparison.FileInfo();
target.filePath = "target_files/pdf/target.pdf";
const options = new comparison.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "output/result.pdf";
// Create comparison request
const request = new comparison.ComparisonsRequest(options);
// Execute comparison
compareApi.comparisons(request)
.then(response => {
console.log("PDF comparison completed successfully!");
console.log(`Result file URL: ${response.href}`);
})
.catch(error => {
console.log("Error:", error.message);
});
Step 5: PDF-Specific Comparison Options
When comparing PDF documents, you can use additional options specific to PDF files:
// PDF-specific comparison options
options.CompareOptions = new CompareOptions
{
// Compare bookmarks in PDFs
CompareBookmarks = true,
// Compare document metadata
CompareDocumentProperties = true,
// Compare backgrounds
CompareBackgrounds = true,
// Other generic options
ShowDeletedContent = true,
ShowInsertedContent = true,
// Highlight style for PDF changes
InsertedItemsStyle = new StyleSettings
{
HighlightColor = "Blue",
FontColor = "Black",
Bold = true
}
};
Step 6: Understanding PDF Comparison Results
After successfully comparing PDF documents, you can:
- Download the result file using the provided URL
- Open the PDF to view the highlighted changes
- Navigate through the document to identify:
- Text changes (additions, deletions, modifications)
- Image differences
- Form field changes
- Formatting differences
The comparison result is a standard PDF file that can be viewed in any PDF reader.
Try It Yourself
Now it’s your turn to practice:
- Upload two different versions of a PDF document to your cloud storage
- Use the provided code examples to compare these PDF files
- Download and examine the comparison result
- Experiment with different comparison options specific to PDFs
- Try comparing PDFs with different types of content (text, images, forms)
Practical Applications for PDF Comparison
PDF comparison is particularly useful in several scenarios:
- Legal Document Review: Compare contract versions in PDF format
- PDF Form Processing: Identify changes in completed PDF forms
- PDF Report Analysis: Compare financial or technical reports
- Academic Documentation: Review changes in research papers or academic PDFs
- PDF Manual Updates: Verify changes between different versions of manuals or guides
Troubleshooting Tips
- Large PDF Files: If comparing large PDFs, increase request timeout settings
- Complex PDF Structure: For PDFs with complex layouts, adjust sensitivity settings
- PDF Security: Ensure PDFs are not password-protected or have restrictions
- PDF Version Compatibility: For best results, compare PDFs of the same version
- Text Recognition Issues: For scanned PDFs, quality of comparison depends on text recognition
What You’ve Learned
In this tutorial, you’ve learned:
- How to compare PDF documents using GroupDocs.Comparison Cloud API
- How to configure PDF-specific comparison options
- How to implement PDF comparison in different programming languages
- How to interpret and work with PDF comparison results
- Practical applications for PDF comparison
Next Steps
Now that you can compare PDF files, you might want to learn how to create previews of documents: