Code samples for PDF to PDF/A Converter
Product update
This product has been replaced by the Pdftools SDK.
PDF to PDF/A Converter converts PDFs to PDF/A format. Here you'll find some examples of how to integrate the code in your development.
Conversion
Convert PDF document to PDF/A and do a post-validation
1// Create the converter
2pConverter = Pdf2PdfCreateObject();
3
4Pdf2PdfSetCompliance(pConverter, ePDFA2b);
5Pdf2PdfSetReportSummary(pConverter, 1);
6
7// The ConversionErrorMask property defines what is crucial to your process and
8// should lead to a conversion error
9Pdf2PdfSetConversionErrorMask(pConverter, (int)ePDFConversionErrorVisualDiff + (int)ePDFConversionErrorActionRemoved +
10 (int)ePDFConversionErrorCorrupt + (int)ePDFConversionErrorDocSigned + (int)ePDFConversionErrorEFRemoved +
11 (int)ePDFConversionErrorFontSubst + (int)ePDFConversionErrorStructureRemoved);
12
13// Get logfile name
14_tcscpy(szCopyFile, szOutputPath);
15szLogFileName = _tcstok(szCopyFile, _T("."));
16_tcscat(szLogFileName, _T("-log.txt"));
17
18// Convert input file to PDF/A
19if (!Pdf2PdfConvert(pConverter, szInputPath, _T(""), szOutputPath, szLogFileName))
20{
21 if (Pdf2PdfGetErrorCode(pConverter) == PDF_E_POSTANALYSIS)
22 {
23 // A post analysis error indicates that the output file is not PDF/A.
24 // A detailed description why the post analysis failed can be found in the log file.
25 _tprintf(_T("The output file %s is not PDF/A compliant. %s (ErrorCode: 0x%08x).\n"), szOutputPath, Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
26 iReturnValue = 1;
27 goto cleanup;
28 }
29 else if (Pdf2PdfGetErrorCode(pConverter) == PDF_E_CONVERSION)
30 {
31 int errors = Pdf2PdfGetConversionErrors(pConverter);
32
33 // Print all conversion errors that occurred during conversion
34 _tprintf(_T("File converted to PDF/A, but the following conversion errors occurred (see TPDFConversionError):\n"));
35
36 for (int i = 1; i <= errors; i = 2 * i)
37 {
38 if ((i & errors) != 0)
39 {
40 _tprintf(_T("- 0x%05x\n"), i);
41 }
42 }
43
44 // Decide if the errors are acceptable
45 _tprintf(_T("\nPlease review the output file and confirm whether it is acceptable: [y/n] \n"));
46
47 scanf("%s", str);
48 if (_tcscmp(str, _T("y")) != 0)
49 {
50 _tprintf(_T("The conversion result was rejected due to conversion errors.\n"));
51 iReturnValue = 1;
52 goto cleanup;
53 }
54 }
55 else
56 {
57 _tprintf(_T("Input file %s could not be converted to PDF/A. %s (ErrorCode: 0x%08x).\n"), szInputPath, Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
58 iReturnValue = 1;
59 goto cleanup;
60 }
61}
62
1// Create the converter
2using (Pdf2Pdf converter = new Pdf2Pdf())
3{
4 converter.Compliance = PDFCompliance.ePDFA2b;
5 converter.ReportSummary = true;
6
7 // The ConversionErrorMask property defines what is crucial to your process and
8 // should lead to a conversion error.
9 converter.ConversionErrorMask = (int)PDFConversionError.ePDFConversionErrorVisualDiff +
10 (int)PDFConversionError.ePDFConversionErrorActionRemoved +
11 (int)PDFConversionError.ePDFConversionErrorCorrupt +
12 (int)PDFConversionError.ePDFConversionErrorDocSigned +
13 (int)PDFConversionError.ePDFConversionErrorEFRemoved +
14 (int)PDFConversionError.ePDFConversionErrorFontSubst +
15 (int)PDFConversionError.ePDFConversionErrorStructureRemoved;
16
17 // Convert input file to PDF/A
18 if (!converter.Convert(inputPath, "", outputPath, Path.ChangeExtension(outputPath, null) + "-log.txt"))
19 {
20 if (converter.ErrorCode == PDFErrorCode.PDF_E_POSTANALYSIS)
21 {
22 // A post analysis error indicates that the output file is not PDF/A.
23 // A detailed description why the post analysis failed can be found in the log file.
24 throw new Exception(String.Format("The output file {0} is not PDF/A compliant. {1} " +
25 "(ErrorCode: 0x{2:x}).", outputPath, converter.ErrorMessage, converter.ErrorCode));
26 }
27 else if (converter.ErrorCode == PDFErrorCode.PDF_E_CONVERSION)
28 {
29 Array errors = Enum.GetValues(typeof(PDFConversionError));
30
31 // Print all conversion errors that occurred during conversion
32 Console.WriteLine("File converted to PDF/A, but the following " +
33 "conversion errors occurred:");
34 foreach (PDFConversionError err in errors)
35 {
36 if (((int)err & converter.ConversionErrors) != 0)
37 Console.WriteLine("- " + err.ToString());
38 }
39 // Decide if the errors are acceptable
40 Console.WriteLine(Environment.NewLine + "Please review the output file and confirm " +
41 "whether it is acceptable: [y/n] ");
42 if (Console.ReadLine().ToLower() != "y")
43 throw new Exception("The conversion result was rejected due to conversion errors.");
44 }
45 else
46 throw new Exception(String.Format("Input file {0} could not be converted to PDF/A. " +
47 "{1} (ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
48 }
49}
50
1// Create the converter
2converter = new Pdf2PdfAPI();
3
4converter.setCompliance(NativeLibrary.COMPLIANCE.ePDFA2b);
5converter.setReportSummary(true);
6
7// The ConversionErrorMask property defines what is crucial to your process and
8// should lead to a conversion error.
9converter.setConversionErrorMask((int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorVisualDiff +
10 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorActionRemoved +
11 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorColorants +
12 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorCorrupt +
13 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorDocSigned +
14 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorEFRemoved +
15 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorFontSubst +
16 (int)NativeLibrary.CONVERSIONERROR.ePDFConversionErrorStructureRemoved);
17
18// Convert input file to PDF/A
19if (!converter.convert(inputPath, "", outputPath, outputPath.replace(".pdf", "") + "-log.txt"))
20{
21 if (converter.getErrorCode() == NativeLibrary.ERRORCODE.PDF_E_POSTANALYSIS)
22 {
23 // A post analysis error indicates that the output file is not PDF/A.
24 // A detailed description why the post analysis failed can be found in the log file.
25 throw new IOException(String.format("The output file %s is not PDF/A compliant. " +
26 "%s (ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(),
27 converter.getErrorCode()));
28 }
29 else if (converter.getErrorCode() == NativeLibrary.ERRORCODE.PDF_E_CONVERSION)
30 {
31 int errors = (converter.getConversionErrorMask() & converter.getConversionErrors());
32
33 // Print all conversion errors that occurred during conversion
34 System.out.println("File converted to PDF/A, but the following " +
35 "conversion errors occurred (see NativeLibrary.CONVERSIONERROR):");
36 for (int i = 1; i <= errors; i = 2*i)
37 {
38 if ((i & errors) != 0)
39 System.out.println(String.format("- 0x%05x", i));
40 }
41 // Decide if the errors are acceptable
42 System.out.println("Please review the output file and confirm whether " +
43 "it is acceptable: [y/n]");
44 while((char) System.in.read() != 'y')
45 throw new IOException("The conversion result was rejected due to conversion errors.");
46 }
47 else
48 throw new IOException(String.format("Input file %s could not be converted to PDF/A. " +
49 "%s (ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(),
50 converter.getErrorCode()));
51}
52
Convert PDF document to PDF/A
1// Create the converter
2pConverter = Pdf2PdfCreateObject();
3
4// Set compliance level
5Pdf2PdfSetCompliance(pConverter, eCompliance);
6Pdf2PdfSetAllowDowngrade(pConverter, 1);
7
8// Convert to PDF/A
9if (!Pdf2PdfConvert(pConverter, szInputPath, _T(""), szOutputPath, _T("")))
10{
11 _tprintf(_T("Input file %s could not be converted. %s (ErrorCode: 0x%08x).\n"), szInputPath, Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
12 iReturnValue = 1;
13}
14
1// Create the converter
2using (Pdf2Pdf converter = new Pdf2Pdf())
3{
4 // Set compliance level
5 converter.Compliance = compliance;
6 converter.AllowDowngrade = true;
7
8 // Convert to PDF/A
9 if (!converter.Convert(inputPath, "", outputPath, ""))
10 throw new Exception(String.Format("Input file {0} could not be converted. " +
11 "{1} (ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
12}
13
1// Create the converter
2converter = new Pdf2PdfAPI();
3
4// Set compliance level
5converter.setCompliance(compliance);
6converter.setAllowDowngrade(true);
7
8// Convert to PDF/A
9if (!converter.convert(inputPath, "", outputPath, ""))
10 throw new Exception(String.format("Input file %s could not be converted. %s " +
11 "(ErrorCode: 0x%08x).", inputPath, converter.getErrorMessage(), converter.getErrorCode()));
12
ZUGFeRD
Create a ZUGFeRD invoice
1// Create the converter
2pConverter = Pdf2PdfCreateObject();
3
4// A ZUGFeRD invoice has to be PDF/A-3 compliant
5Pdf2PdfSetCompliance(pConverter, ePDFA3u);
6Pdf2PdfSetAllowDowngrade(pConverter, 1);
7
8// Add ZUGFeRD invoice
9if (!Pdf2PdfAddInvoiceXml(pConverter, ePDFInvoiceZugferd, szInvoicePath, NULL))
10{
11 _tprintf(_T("ZUGFeRD invoice %s could not be added. %s (ErrorCode: 0x%08x).\n"), szInvoicePath, Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
12 iReturnValue = 1;
13 goto cleanup;
14}
15
16// Convert PDF to PDF/A
17if (!Pdf2PdfConvert(pConverter, szInputPath, _T(""), szOutputPath, _T("")))
18{
19 _tprintf(_T("Input file %s could not be converted. %s (ErrorCode: 0x%08x).\n"), szInputPath, Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
20 iReturnValue = 1;
21}
22
1// Create the converter
2using (Pdf2Pdf converter = new Pdf2Pdf())
3{
4 // A ZUGFeRD invoice has to be PDF/A-3 compliant
5 converter.Compliance = PDFCompliance.ePDFA3u;
6 converter.AllowDowngrade = true;
7
8 // Add ZUGFeRD invoice
9 if (!converter.AddInvoiceXml(PDFInvoiceType.ePDFInvoiceZugferd, invoicePath, null))
10 throw new Exception(String.Format("ZUGFeRD invoice {0} could not be added. " +
11 "{1} (ErrorCode: 0x{2:x}).", invoicePath, converter.ErrorMessage, converter.ErrorCode));
12
13 // Convert PDF to PDF/A
14 if (!converter.Convert(inputPath, "", outputPath, ""))
15 throw new Exception(String.Format("Input file {0} could not be converted. " +
16 "{1} (ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
17}
18
1// Create the converter
2converter = new Pdf2PdfAPI();
3
4// A ZUGFeRD invoice has to be PDF/A-3 compliant
5converter.setCompliance(NativeLibrary.COMPLIANCE.ePDFA3u);
6converter.setAllowDowngrade(true);
7
8// Add ZUGFeRD invoice
9if (!converter.addInvoiceXml(NativeLibrary.PDFINVOICETYPE.ePDFInvoiceZugferd, invoicePath, null))
10 throw new IOException(String.format("ZUGFeRD invoice %s could not be added. " +
11 "%s (ErrorCode: 0x%08x).", invoicePath, converter.getErrorMessage(),
12 converter.getErrorCode()));
13
14// Convert PDF to PDF/A
15if (!converter.convert(inputPath, "", outputPath, ""))
16 throw new IOException(String.format("Input file %s could not be converted. %s " +
17 "(ErrorCode: 0x%08x).", inputPath, converter.getErrorMessage(), converter.getErrorCode()));
18
Signing
Convert PDF document to PDF/A and add a signature
1// Create the converter
2pConverter = Pdf2PdfCreateObject();
3
4// Set compliance
5Pdf2PdfSetCompliance(pConverter, ePDFA2b);
6
7// Begin session with Windows cryptographic provider
8if (!Pdf2PdfBeginSession(pConverter, ""))
9{
10 _tprintf(_T("Unable to connect to Windows cryptographic provider. %s (ErrorCode: 0x%08x).\n"), Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
11 iReturnValue = 1;
12 goto cleanup;
13}
14
15// Create signature object
16pSignature = Pdf2PdfSignatureCreateObject();
17
18Pdf2PdfSignatureSetName(pSignature, szCertificate);
19Pdf2PdfAddSignature(pConverter, pSignature);
20Pdf2PdfSignatureDestroyObject(pSignature);
21
22// Create PDF/A and sign it
23if (!Pdf2PdfConvert(pConverter, szInputPath, _T(""), szOutputPath, _T("")))
24{
25 _tprintf(_T("Input file %s could not be converted. %s (ErrorCode: 0x%08x).\n"), szInputPath, Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
26 iReturnValue = 1;
27}
28
1// Create the converter
2using (Pdf2Pdf converter = new Pdf2Pdf())
3{
4 // Set compliance
5 converter.Compliance = PDFCompliance.ePDFA2b;
6
7 // Begin session with Windows cryptographic provider
8 if (!converter.BeginSession(""))
9 throw new Exception(String.Format("Unable to connect to Windows cryptographic provider. " +
10 "{0} (ErrorCode: 0x{1:x}).", converter.ErrorMessage, converter.ErrorCode));
11
12 // Create signature object
13 using (Signature signature = new Signature())
14 {
15 signature.Name = certificate;
16 converter.AddSignature(signature);
17
18 // Create PDF/A and sign it
19 if (!converter.Convert(inputPath, "", outputPath, ""))
20 throw new Exception(String.Format("Input file {0} could not be converted. {1} " +
21 "(ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
22 }
23
24 // Cleanup
25 converter.EndSession();
26 Pdf2Pdf.Terminate();
27}
28
1// Create the converter
2converter = new Pdf2PdfAPI();
3
4// Set compliance
5converter.setCompliance(NativeLibrary.COMPLIANCE.ePDFA2b);
6
7// Begin session with Windows cryptographic provider
8if (!converter.beginSession(""))
9 throw new Exception(String.format("Unable to connect to Windows cryptographic provider. " +
10 "%s (ErrorCode: 0x%08x).", converter.getErrorMessage(), converter.getErrorCode()));
11
12// Create signature object
13signature = new Signature();
14
15signature.setName(certificate);
16converter.addSignature(signature);
17
18// Create PDF/A and sign it
19if (!converter.convert(inputPath, "", outputPath, ""))
20 throw new IOException(String.format("Input file %s could not be converted. %s " +
21 "(ErrorCode: 0x%08x)", inputPath, converter.getErrorMessage(), converter.getErrorCode()));
22
23// Clean up
24converter.endSession();
25
In Memory
Convert PDF document to PDF/A in memory
1// Create the converter
2pConverter = Pdf2PdfCreateObject();
3
4// Convert input PDF byte array to output PDF/A byte array, get log file as byte array
5if (!Pdf2PdfConvertMem(pConverter, pInputBuffer, nLength, _T(""), &pOutputBuffer, &nOutLength, &pLogBuffer, &nLogLength))
6{
7 _tprintf(_T("File could not be converted to PDF/A. %s (ErrorCode: 0x%08x).\n"), Pdf2PdfGetErrorMessage(pConverter), Pdf2PdfGetErrorCode(pConverter));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Write bytes to output file
13pData = _tfopen(szOutputPath, _T("wb"));
14fwrite(pOutputBuffer, 1, nOutLength, pData);
15fclose(pData);
16
17// Write bytes to log file
18pLog = _tfopen(szLogPath, _T("wb"));
19fwrite(pLogBuffer, 1, nLogLength, pLog);
20fclose(pLog);
21
1// Create the converter
2using (Pdf2Pdf converter = new Pdf2Pdf())
3{
4 // Convert input PDF byte array to output PDF/A byte array, get log file as byte array
5 if (!converter.ConvertMem(inputBuffer, "", out outputBuffer, out logBuffer))
6 throw new Exception(String.Format("File could not be converted to PDF/A. " +
7 "{0} (ErrorCode: 0x{1:x}).", converter.ErrorMessage, converter.ErrorCode));
8}
9
10// Write bytes to output file
11File.WriteAllBytes(outputPath, outputBuffer);
12// Write bytes to log file
13File.WriteAllBytes(logPath, logBuffer);
14
1// Create the converter
2converter = new Pdf2PdfAPI();
3
4// Convert input PDF byte array to output PDF/A byte array, get log file as byte array
5if (!converter.convertMem2(inputBuffer, ""))
6 throw new IOException(String.format("PDF/A could not be created. %s (ErrorCode: 0x%08x).",
7 converter.getErrorMessage(), converter.getErrorCode()));
8
9// Get PDF as buffer
10byte[] outputBuffer = converter.getPDF();
11if (outputBuffer == null)
12 throw new IOException(String.format("Output file %s cannot be converted as a byte array. %s " +
13 "(ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(), converter.getErrorCode()));
14
15// Get output log as Buffer
16byte[] logBuffer = converter.getLog();
17
18// Write bytes to output file
19Files.write(Paths.get(outputPath), outputBuffer);
20
21// Write bytes to log file
22Files.write(Paths.get(logPath), logBuffer);
23