VisualComparisonTest

Sample C# code for using Apryse SDK to overlay two PDF files allowing for a visual color difference between them. This is commonly used to see changes between two document revisions or in drawing blueprints that are overlayed with updated data.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14
15namespace PDFNetSamples
16{
17 public sealed class VisualComparisionTest : Sample
18 {
19 public VisualComparisionTest() :
20 base("VisualComparision", "PDFNet can generate a visual comparison between two different documents. The comparison result itself is a resolution independent PDF -- well suited for adding high-quality diff support into existing app workflows.")
21 {
22 }
23
24 public override IAsyncAction RunAsync()
25 {
26 return Task.Run(new System.Action(async () => {
27 WriteLine("--------------------------------");
28 WriteLine("Starting Visual Comparision Test...");
29 WriteLine("--------------------------------\n");
30
31 bool error = await DiffingTestAsync();
32
33 if (error)
34 {
35 WriteLine("Visual Comparision failed");
36 }
37 else
38 {
39 WriteLine("Visual Comparision succeeded");
40 }
41
42 WriteLine("\n--------------------------------");
43 WriteLine("Done Visual Comparision Test.");
44 WriteLine("--------------------------------\n");
45 })).AsAsyncAction();
46 }
47
48 private async Task<bool> DiffingTestAsync()
49 {
50 bool error = false;
51
52 string firstDocumentPath = Path.Combine(InputPath, "diff_doc_1.pdf");
53 string secondDocumentPath = Path.Combine(InputPath, "diff_doc_2.pdf");
54 string outputDocumentPath = Path.Combine(OutputPath, "VisualComparision.pdf");
55
56 if ((!File.Exists(firstDocumentPath)) || (!File.Exists(secondDocumentPath)))
57 {
58 WriteLine("Atleast one input file does not exist. Aborting.");
59 error = true;
60 return error;
61 }
62
63 try
64 {
65 using (PDFDoc firstDocument = new PDFDoc(firstDocumentPath))
66 using (PDFDoc secondDocument = new PDFDoc(secondDocumentPath))
67 {
68 Page firstDocumentPage = firstDocument.GetPage(1);
69 Page secondDocumentPage = secondDocument.GetPage(1);
70
71 DiffOptions diffOptions = new DiffOptions();
72 diffOptions.SetColorA(new ColorPt(1, 0, 0));
73 diffOptions.SetColorB(new ColorPt(0, 0, 0));
74 diffOptions.SetBlendMode(GStateBlendMode.e_bl_normal);
75
76 PDFDoc outputDocument = new PDFDoc();
77 outputDocument.AppendVisualDiff(firstDocumentPage, secondDocumentPage, diffOptions);
78 await outputDocument.SaveAsync(outputDocumentPath, SDFDocSaveOptions.e_linearized);
79 await AddFileToOutputList(outputDocumentPath).ConfigureAwait(false);
80 }
81 }
82 catch (Exception e)
83 {
84 WriteLine(GetExceptionMessage(e));
85 error = true;
86 }
87
88 return error;
89 }
90 }
91}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales