1//
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.SDF;
8using pdftron.PDF;
9using pdftron.PDF.PDFA;
10
11//-----------------------------------------------------------------------------------
12// The sample illustrates how to use PDF/A related API-s.
13//-----------------------------------------------------------------------------------
14using NUnit.Framework;
15
16namespace MiscellaneousSamples
17{
18	[TestFixture]
19	public class PDFATest
20	{
21		
22		// Relative path to the folder containing test files.
23		const string input_path =  "TestFiles/";
24
25		/// <summary>
26		/// The main entry point for the application.
27		/// </summary>
28		[Test]
29		public static void Sample()
30		{
31			PDFNet.SetColorManagement(PDFNet.CMSType.e_lcms);  // Required for PDFA validation.
32
33			//-----------------------------------------------------------
34			// Example 1: PDF/A Validation
35			//-----------------------------------------------------------
36			try
37			{
38				string filename = "newsletter.pdf";
39				using (PDFACompliance pdf_a = new PDFACompliance(false, Utils.GetAssetTempFile(input_path+filename), null, PDFACompliance.Conformance.e_Level2B, null, 10, false))
40				{
41					PrintResults(pdf_a, filename);
42				}
43			}
44			catch (pdftron.Common.PDFNetException e)
45			{
46				Console.WriteLine(e.Message);
47				Assert.True(false);
48			}
49
50			//-----------------------------------------------------------
51			// Example 2: PDF/A Conversion
52			//-----------------------------------------------------------
53			try
54			{
55				string filename = "fish.pdf";
56				using (PDFACompliance pdf_a = new PDFACompliance(true, Utils.GetAssetTempFile(input_path+filename), null, PDFACompliance.Conformance.e_Level2B, null, 10, false))
57				{
58					filename = "pdfa.pdf";
59					pdf_a.SaveAs(Utils.CreateExternalFile(filename), false);
60				}
61
62				// Re-validate the document after the conversion...
63                filename = "pdfa.pdf";
64				using (PDFACompliance pdf_a = new PDFACompliance(false, Utils.CreateExternalFile(filename), null, PDFACompliance.Conformance.e_Level2B, null, 10, false))
65				{
66					PrintResults(pdf_a, filename);				
67				}
68			}
69			catch (pdftron.Common.PDFNetException e)
70			{
71				Console.WriteLine(e.Message);
72				Assert.True(false);
73			}
74            Console.WriteLine("PDFACompliance test completed.");
75
76        }
77
78		static void PrintResults(PDFACompliance pdf_a, String filename) 
79		{
80			int err_cnt = pdf_a.GetErrorCount();
81			if (err_cnt == 0) 
82			{
83				Console.WriteLine("{0}: OK.", filename);
84			}
85			else 
86			{
87				Console.WriteLine("{0} is NOT a valid PDFA.", filename);
88				for (int i=0; i<err_cnt; ++i) 
89				{
90					PDFACompliance.ErrorCode c = pdf_a.GetError(i);
91					Console.WriteLine(" - e_PDFA {0}: {1}.", 
92						(int)c, PDFACompliance.GetPDFAErrorMessage(c));
93
94					if (true) 
95					{
96						int num_refs = pdf_a.GetRefObjCount(c);
97						if (num_refs > 0)  
98						{
99							Console.Write("   Objects: ");
100							for (int j=0; j<num_refs; ) 
101							{
102								Console.Write("{0}", pdf_a.GetRefObj(c, j));
103								if (++j!=num_refs) Console.Write(", ");
104							}
105							Console.WriteLine();
106						}
107					}
108				}
109				Console.WriteLine();
110			}
111		}
112	}
113}