You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: net/advanced-operations/compare-pdf-documents/_index.md
+133-5Lines changed: 133 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,21 +11,23 @@ sitemap:
11
11
priority: 0.7
12
12
---
13
13
14
-
##Comparing PDF Documents with Aspose.PDF for .NET
14
+
# Comparing PDF Documents with Aspose.PDF for .NET
15
15
16
16
When working with PDF documents, there are times when you need to compare the content of two documents to identify differences. The Aspose.PDF for .NET library provides a powerful toolset for this purpose. In this article, we'll explore how to compare PDF documents using a couple of simple code snippets.
17
17
18
18
The comparison functionality in Aspose.PDF allows you to compare two PDF documents page by page. You can choose to compare either specific pages or entire documents. The resulting comparison document highlights differences, making it easier to identify changes between the two files.
19
19
20
-
###Comparing Specific Pages
20
+
## Comparing Specific Pages
21
21
22
22
The first code snippet demonstrates how to compare the first pages of two PDF documents.
23
23
24
24
Steps:
25
25
26
26
1. Document Initialization.
27
27
The code starts by initializing two PDF documents using their respective file paths (documentPath1 and documentPath2). The paths are specified as empty strings for now, but in practice, you would replace these with the actual file paths.
28
+
28
29
2. Comparison Process.
30
+
29
31
- Page Selection - the comparison is limited to the first page of each document ('Pages[1]').
30
32
- Comparison Options:
31
33
@@ -52,17 +54,19 @@ The code starts by initializing two PDF documents using their respective file pa
52
54
}
53
55
```
54
56
55
-
56
-
### Comparing Entire Documents
57
+
## Comparing Entire Documents
57
58
58
59
The second code snippet expands the scope to compare the entire content of two PDF documents.
59
60
60
61
Steps:
61
62
62
63
1. Document Initialization.
63
64
Just like in the first example, two PDF documents are initialized with their file paths.
65
+
64
66
2. Comparison Process.
67
+
65
68
- Entire Document Comparison - unlike the first snippet, this code compares the entire content of the two documents.
69
+
66
70
- Comparison Options - the options are the same as in the first snippet, ensuring that spaces are ignored, and additional change markers are displayed.
67
71
68
72
3. The comparison result, which highlights differences across all pages of the two documents, is saved in the file specified by 'resultPdfPath'.
@@ -90,4 +94,128 @@ The comparison results generated by these snippets are PDF documents that you ca
90
94
91
95
By setting 'AdditionalChangeMarks' to 'true', you can also see markers for changes that may occur on other pages, even if those changes aren't on the current page being viewed.
92
96
93
-
**Aspose.PDF for .NET** provides robust tools for comparing PDF documents, whether you need to compare specific pages or entire documents. By using options like 'AdditionalChangeMarks' and different 'ComparisonMode settings', you can tailor the comparison process to your specific needs. The resulting document provides a clear, side-by-side view of changes, making it easier to track revisions and ensure document accuracy.
97
+
**Aspose.PDF for .NET** provides robust tools for comparing PDF documents, whether you need to compare specific pages or entire documents. By using options like 'AdditionalChangeMarks' and different 'ComparisonMode settings', you can tailor the comparison process to your specific needs. The resulting document provides a clear, side-by-side view of changes, making it easier to track revisions and ensure document accuracy.
98
+
99
+
## Compare PDF documents using GraphicalPdfComparer
100
+
101
+
The following code snippets also work with [Aspose.PDF.Drawing](https://docs.aspose.com/pdf/net/drawing/) library.
102
+
103
+
When collaborating on documents, especially in professional environments, you often end up with multiple versions of the same file.
104
+
105
+
You can use the [GraphicalPdfComparer](https://reference.aspose.com/pdf/net/aspose.pdf.comparison.graphicalcomparison/graphicalpdfcomparer/) class to compare PDF documents and pages. The class is suitable for comparing changes in a page's graphic content.
106
+
107
+
With Aspose.PDF for .NET, it's possible to compare documents and pages and output the comparison result to a PDF document or image file.
108
+
109
+
You can set the following class properties:
110
+
111
+
- Resolution - resolution in DPI units for output images, as well as for images generated during the comparison.
112
+
- Color - the color of change marks.
113
+
- Threshold - change threshold in percent. The default value is zero. Setting a value other than zero allows you to ignore graphic changes that are insignificant to you.
114
+
115
+
The class has a method that allows you to get page image differences in a form suitable for further processing: **ImagesDifference GetDifference(Page page1, Page page2)**.
116
+
117
+
This method returns an object of the [ImagesDifference](https://reference.aspose.com/pdf/net/aspose.pdf.comparison.graphicalcomparison/imagesdifference/) class, which contains an image of the first page being compared and an array of differences. The array of differences and the original image has the **RGB24bpp** pixel format.
118
+
119
+
ImagesDifference allows you to generate a different image and get an image of the second page being compared by adding an array of differences to the original image. To do this, use the **ImagesDifference.GetDestinationImage and ImagesDifference.DifferenceToImage** methods.
120
+
121
+
### Compare PDF with GetDifference method
122
+
123
+
The provided code defines a method [GetDifference](https://reference.aspose.com/pdf/net/aspose.pdf.comparison.graphicalcomparison/imagesdifference/#methods) that compares two PDF documents and generates visual representations of the differences between them.
124
+
125
+
This method compares the first pages of two PDF files and generates two PNG images:
126
+
127
+
- One image (diffPngFilePath) highlights the differences between the pages in red.
128
+
- The other image (destPngFilePath) is a visual representation of the destination (second) PDF page.
129
+
130
+
This process can be useful for visually comparing changes or differences between two versions of a document.
131
+
132
+
```cs
133
+
134
+
stringdoc1Path="";
135
+
stringdoc2Path="";
136
+
stringdestPngFilePath="";
137
+
stringdiffPngFilePath="";
138
+
139
+
using (Documentdoc1=newDocument(doc1Path), doc2=newDocument(doc2Path))
using (ImagesDifferenceimagesDifference=comparer.GetDifference(doc1.Pages[1], doc2.Pages[1]))
171
+
{
172
+
173
+
using (PdfImagediffImg=imagesDifference.DifferenceToImage(Color.Red, Color.White))
174
+
{
175
+
using (FileStreamfileStream=newFileStream(diffPngFilePath, FileMode.Create, FileAccess.Write))
176
+
{
177
+
using (varimgStream=diffImg.BitmapStream)
178
+
{
179
+
imgStream.Seek(0, SeekOrigin.Begin);
180
+
diffImg.BitmapStream.CopyTo(fileStream);
181
+
}
182
+
}
183
+
}
184
+
185
+
186
+
using (PdfImagedestImg=imagesDifference.GetDestinationImage())
187
+
{
188
+
using (FileStreamfileStream=newFileStream(destPngFilePath, FileMode.Create, FileAccess.Write))
189
+
{
190
+
using (varimgStream=destImg.BitmapStream)
191
+
{
192
+
imgStream.Seek(0, SeekOrigin.Begin);
193
+
destImg.BitmapStream.CopyTo(fileStream);
194
+
}
195
+
}
196
+
}
197
+
198
+
}
199
+
}
200
+
```
201
+
202
+
### Compare PDF with CompareDocumentsToPdf method
203
+
204
+
The provided code snippet used the [CompareDocumentsToPdf](https://reference.aspose.com/pdf/net/aspose.pdf.comparison.graphicalcomparison/graphicalpdfcomparer/comparedocumentstopdf/) method, which compares two documents and generates a PDF report of the comparison results.
205
+
206
+
```cs
207
+
208
+
stringfirstPath="";
209
+
stringsecondPath="";
210
+
stringresultPdfPath="";
211
+
using (Documentdoc1=newDocument(firstPath), doc2=newDocument(secondPath))
0 commit comments