Fill PDF forms with XFDF data
This code uses the OpenService
and CloseService
methods from PDF Converter API - Document Converter Service Client sample code and fills a PDF form with data from an XFDF file.
/// <summary> /// Fill the PDF form <paramref name="templateFileName"/> with data from the XFDF file <paramref name="sourceFile"/> /// </summary> /// <param name="sourceFile">XFDF data file</param> /// <param name="templateFileName">PDF Form template</param> /// <param name="serviceURL">Document Converter Services end point</param> /// <param name="targetFolder">Destination folder</param> static void FillPDFForm(string sourceFile, string templateFileName, string serviceURL, string targetFolder) { DocumentConverterServiceClient client = null; try { // ** Determine the source file and read it into a byte array. Console.WriteLine($"Reading {sourceFile}"); byte[] dataFile = File.ReadAllBytes(sourceFile); // ** Determine the template file and read it into a byte array. Console.WriteLine($"Reading {templateFileName}"); byte[] templateFile = File.ReadAllBytes(templateFileName); // ** Open the service and configure the bindings client = OpenService(serviceURL, 255); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFile); openOptions.FileExtension = Path.GetExtension(sourceFile); // ** Create converter specific settings object and specify template data ConverterSpecificSettings_PdfFormsDataImporter pdfFormsDataImporterSettings = new ConverterSpecificSettings_PdfFormsDataImporter(); pdfFormsDataImporterSettings.PdfTemplateData = templateFile; // ** Set the absolute minimum conversion settings. Also, use the converter specific settings defined above ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Format = OutputFormat.PDF; conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Quality = ConversionQuality.OptimizeForPrint; conversionSettings.ConverterSpecificSettings = pdfFormsDataImporterSettings; conversionSettings.PDFProfile = PDFProfile.PDF_A2B; // ** Carry out the import. Console.WriteLine("Importing data file '" + Path.GetFileName(sourceFile) + "' into template file '" + Path.GetFileName(templateFileName) + "'"); byte[] convFile = client.Convert(dataFile, openOptions, conversionSettings); // ** Write the result back to the file system with a PDF extension. string destinationFileName = Path.GetFullPath(Path.Combine(targetFolder, $"{Path.GetFileNameWithoutExtension(sourceFile)}.{conversionSettings.Format}")); using (FileStream fs = File.Create(destinationFileName)) { fs.Write(convFile, 0, convFile.Length); fs.Close(); } Console.WriteLine($"Result saved to {destinationFileName}"); // ** Open the generated PDF file in a PDF Reader Console.WriteLine("Launching file in PDF Reader"); Process.Start(destinationFileName); } catch (FaultException<WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } }