PDF Toolkit

Samples

Convert And Validate PDF/A files

This Code sample requires the PDF Toolkit and is found in: C:\Aquaforest\PDF Toolkit\samples\PDFAConversionAndValidation


using Aquaforest.PDF;
using System;
    
namespace PDFAConversionAndValidation
{
    internal class PdfaOperations
    {
        private static void Main(string[] args)
        {
            //Assign Licence Key
            PDFToolkit.LicenseKey = string.Empty;
            string inputFile = @"..\documents\source\image_pdf.pdf";
            string outputFile = @"..\documents\output\pdfa.pdf";
    
            //Converting PDF File to PDF
            ConvertToPDFA(inputFile, outputFile, AquaforestPDFAFlavour.PDFA_1_B);
    
            //Validate PDFa File
            PDFAValidationResult results = ValidatePDFA(AquaforestPDFAFlavour.PDFA_1_B, outputFile);
    
            //check the result
            if (results.IsValid)
            {
                //Success
                Console.WriteLine("Validated!");
            }
            else
            {
                //Not validated, print out validation errors
                foreach (var error in results.ValidationResult)
                {
                    Console.WriteLine(error);
                }
            }
        }
    
        public static void ConvertToPDFA(string input, string output, AquaforestPDFAFlavour flavour)
        {
            try
            {
                //Create PDFA object
                PDF2PDFaConverter pdfaConverter = new PDF2PDFaConverter(input, output, flavour);
                //Convert File to PDFa
                pdfaConverter.ConvertToPDFA();
            }
            catch (Exception e)
            {
            }
        }
    
        public static PDFAValidationResult ValidatePDFA(AquaforestPDFAFlavour flavour, string file)
        {
            try
            {
                //Create PDFAValidator object
                PDFAValidator pdfaValidator = new PDFAValidator();
                // Validate file base on flavour
                return pdfaValidator.ValidatePDFA(file, flavour);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}