Skip to content

Commit 474efd2

Browse files
committed
Merge branch 'holub' into develop
2 parents 8fb61d7 + 54407d4 commit 474efd2

File tree

1 file changed

+133
-5
lines changed
  • net/advanced-operations/compare-pdf-documents

1 file changed

+133
-5
lines changed

net/advanced-operations/compare-pdf-documents/_index.md

Lines changed: 133 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ sitemap:
1111
priority: 0.7
1212
---
1313

14-
## Comparing PDF Documents with Aspose.PDF for .NET
14+
# Comparing PDF Documents with Aspose.PDF for .NET
1515

1616
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.
1717

1818
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.
1919

20-
### Comparing Specific Pages
20+
## Comparing Specific Pages
2121

2222
The first code snippet demonstrates how to compare the first pages of two PDF documents.
2323

2424
Steps:
2525

2626
1. Document Initialization.
2727
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+
2829
2. Comparison Process.
30+
2931
- Page Selection - the comparison is limited to the first page of each document ('Pages[1]').
3032
- Comparison Options:
3133

@@ -52,17 +54,19 @@ The code starts by initializing two PDF documents using their respective file pa
5254
}
5355
```
5456

55-
56-
### Comparing Entire Documents
57+
## Comparing Entire Documents
5758

5859
The second code snippet expands the scope to compare the entire content of two PDF documents.
5960

6061
Steps:
6162

6263
1. Document Initialization.
6364
Just like in the first example, two PDF documents are initialized with their file paths.
65+
6466
2. Comparison Process.
67+
6568
- Entire Document Comparison - unlike the first snippet, this code compares the entire content of the two documents.
69+
6670
- Comparison Options - the options are the same as in the first snippet, ensuring that spaces are ignored, and additional change markers are displayed.
6771

6872
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
9094

9195
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.
9296

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+
string doc1Path = "";
135+
string doc2Path = "";
136+
string destPngFilePath = "";
137+
string diffPngFilePath = "";
138+
139+
using (Document doc1 = new Document(doc1Path), doc2 = new Document(doc2Path))
140+
{
141+
GraphicalPdfComparer comparer = new GraphicalPdfComparer();
142+
using (ImagesDifference imagesDifference = comparer.GetDifference(doc1.Pages[1], doc2.Pages[1]))
143+
{
144+
145+
using (Bitmap diffImg = imagesDifference.DifferenceToImage(Color.Red, Color.White))
146+
{
147+
diffImg.Save(diffPngFilePath);
148+
}
149+
150+
using (Bitmap destImg = imagesDifference.GetDestinationImage())
151+
{
152+
destImg.Save(destPngFilePath);
153+
}
154+
}
155+
}
156+
```
157+
158+
The provided code snippet used the [Aspose.PDF.Drawing](https://docs.aspose.com/pdf/net/drawing/) library and compares two PDF documents visually.
159+
160+
```cs
161+
162+
string doc1Path = "";
163+
string doc2Path = "";
164+
string destPngFilePath = "";
165+
string diffPngFilePath = "";
166+
167+
using (Document doc1 = new Document(doc1Path), doc2 = new Document(doc2Path))
168+
{
169+
GraphicalPdfComparer comparer = new GraphicalPdfComparer();
170+
using (ImagesDifference imagesDifference = comparer.GetDifference(doc1.Pages[1], doc2.Pages[1]))
171+
{
172+
173+
using (PdfImage diffImg = imagesDifference.DifferenceToImage(Color.Red, Color.White))
174+
{
175+
using (FileStream fileStream = new FileStream(diffPngFilePath, FileMode.Create, FileAccess.Write))
176+
{
177+
using (var imgStream = diffImg.BitmapStream)
178+
{
179+
imgStream.Seek(0, SeekOrigin.Begin);
180+
diffImg.BitmapStream.CopyTo(fileStream);
181+
}
182+
}
183+
}
184+
185+
186+
using (PdfImage destImg = imagesDifference.GetDestinationImage())
187+
{
188+
using (FileStream fileStream = new FileStream(destPngFilePath, FileMode.Create, FileAccess.Write))
189+
{
190+
using (var imgStream = 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+
string firstPath = "";
209+
string secondPath = "";
210+
string resultPdfPath = "";
211+
using (Document doc1 = new Document(firstPath), doc2 = new Document(secondPath))
212+
{
213+
GraphicalPdfComparer comparer = new GraphicalPdfComparer()
214+
{
215+
Threshold = 3.0,
216+
Color = Color.Blue,
217+
Resolution = new Resolution(300)
218+
};
219+
comparer.CompareDocumentsToPdf(doc1, doc2, resultPdfPath);
220+
}
221+
```

0 commit comments

Comments
 (0)