public void ManipulatePdf() { // Create a pdf document instance PdfDocument pdfDoc = new PdfDocument(new PdfReader(INPUT_PDF), new PdfWriter(DEST)); // Load the DOM Document XfaForm xfa = PdfAcroForm.GetAcroForm(pdfDoc, false).GetXfaForm(); XDocument domDoc = xfa.GetDomDocument(); // This works for the specific document // Generate the list of calculate amount Nodes IEnumerable <XElement> calcElements = findCalc(domDoc); // Update calculate value foreach (XElement element in calcElements) { XElement calc = element.Descendants().First(); String curVal = calc.Value; calc.Value = "2*" + curVal; Console.WriteLine(calc.Value); } // Write XFA back to PDF Document xfa.SetDomDocument(domDoc); xfa.Write(pdfDoc); pdfDoc.Close(); }
public static bool GetXFAfieldValue(String path, String FieldName) { if (!File.Exists(path)) { Report.Warn("File " + path + " does not exist, please check file path!"); return(false); } PdfReader newreader = new PdfReader(path); PdfDocument pdfDoc = new PdfDocument(newreader); XfaForm xfa = new XfaForm(pdfDoc); if (xfa.IsXfaPresent()) { XDocument myXMLDoc = xfa.GetDomDocument(); IEnumerable <XElement> mylist = myXMLDoc.Descendants(FieldName); List <XElement> mylistt = mylist.ToList <XElement>(); Report.Info(mylistt[0].Value); // or if more than one, you can cycle through: /*** * foreach (XElement theEl in mylist){ * Report.Info(theEl.Value); * } ***/ return(true); } else { Report.Warn("This is not an XFA document"); return(false); } }
public void ManipulatePdf() { // Create a PDF document instance PdfDocument pdfDoc = new PdfDocument(new PdfReader(INPUT_PDF), new PdfWriter(DEST)); // Load the DOM Document XfaForm xfa = PdfAcroForm.GetAcroForm(pdfDoc, false).GetXfaForm(); XDocument domDoc = xfa.GetDomDocument(); // The follwing 2 lines of code only work for the specific document // Access the Script Node of the DOM Document XElement template = domDoc.Descendants().First().Descendants().First().ElementsAfterSelf().First(); XElement script = template.Descendants().First().Descendants().First().ElementsAfterSelf().First() .Descendants().First().ElementsAfterSelf().First().ElementsAfterSelf().First().ElementsAfterSelf() .First().ElementsAfterSelf().First().ElementsAfterSelf().First().Descendants().First(); // Update the script message String message = "xfa.host.messageBox(\"XFA SCRIPT Message!!!\")"; script.SetValue(message); // Write XFA back to PDF Document xfa.SetDomDocument(domDoc); xfa.Write(pdfDoc); pdfDoc.Close(); }
public void ManipulatePdf() { // Create a pdf document instance PdfDocument pdfDoc = new PdfDocument(new PdfReader(INPUT_PDF), new PdfWriter(DEST)); // Load the DOM Document XfaForm xfa = PdfAcroForm.GetAcroForm(pdfDoc, false).GetXfaForm(); XDocument domDoc = xfa.GetDomDocument(); // The follwing 2 lines of code only work for the specific document // Access the Script Node of the DOM Document XElement template = domDoc.Descendants().First().Descendants().First().ElementsAfterSelf().First(); XElement script = template.Descendants().First().Descendants().First().ElementsAfterSelf().First() .Descendants().First().ElementsAfterSelf().First().ElementsAfterSelf().First().ElementsAfterSelf() .First().ElementsAfterSelf().First().ElementsAfterSelf().First().Descendants().First(); // Remove the Script from the Node script.SetValue(""); // Write XFA back to the PDF Document xfa.SetDomDocument(domDoc); xfa.Write(pdfDoc); pdfDoc.Close(); }
/// <summary> /// Performs the playback of actions in this module. /// </summary> /// <remarks>You should not call this method directly, instead pass the module /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method /// that will in turn invoke this method.</remarks> void ITestModule.Run() { Mouse.DefaultMoveTime = 300; Keyboard.DefaultKeyPressTime = 100; Delay.SpeedFactor = 1.0; // This is dependent on the itext 7 community NuGet package reference string src = @"C:\Users\edgewords\Documents\czechexamples\Property_contract_sample.pdf"; //Read PDF PdfReader reader = new PdfReader(src); PdfDocument pdf = new PdfDocument(reader); string text = string.Empty; //Iterate over all pages for (int page = 1; page <= pdf.GetNumberOfPages(); page++) { PdfPage myPage = pdf.GetPage(page); //Extract plain text text += PdfTextExtractor.GetTextFromPage(myPage); } reader.Close(); //Find specific text int pos = text.IndexOf("Spoluúčast") + 10; //Get next 10 characters Ranorex.Report.Info(text.Substring(pos, 10)); //*************************// // Now for the word document. Open the word document, then save as text only, you could then open it and read from it // using normal System.IO library Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); Document document = null; application.Visible = false; object missing = System.Reflection.Missing.Value; object readOnly = false; document = application.Documents.Open(@"C:\Users\edgewords\Documents\czechexamples\Property_policy_sample.doc", ref missing, ref readOnly); //document.ExportAsFixedFormat(@"C:\Users\edgewords\Documents\czechexamples\Property_policy_sample.pdf", WdExportFormat.wdExportFormatPDF); document.SaveAs(@"C:\Users\edgewords\Documents\czechexamples\Property_policy_sample.txt", 2); Ranorex.Report.Info(document.Range(1, document.Characters.Count).Text); application.ActiveDocument.Close(); application.Quit(); // Try to open the Xfa dynamic acrobat form as xml document // https://csharp.hotexamples.com/examples/iTextSharp.text.pdf/XfaForm/-/php-xfaform-class-examples.html PdfReader newreader = new PdfReader(@"C:\Users\edgewords\Documents\czechexamples\eForm_example.pdf"); PdfDocument pdfDoc = new PdfDocument(newreader); XfaForm xfa = new XfaForm(pdfDoc); if (xfa.IsXfaPresent()) { XDocument myXMLDoc = xfa.GetDomDocument(); IEnumerable <XElement> mylist = myXMLDoc.Descendants("CONTRACT_ID"); List <XElement> mylistt = mylist.ToList <XElement>(); Report.Info(mylistt[0].Value); // or if more than one, you can cycle through: /*** * foreach (XElement theEl in mylist){ * Report.Info(theEl.Value); * } * ***/ } else { PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); IDictionary <String, PdfFormField> fields = form.GetFormFields(); PdfFormField toSet; fields.TryGetValue("CONTRACT_ID", out toSet); Report.Info(toSet.GetValueAsString()); } //reader.Close(); }