Example #1
0
        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);
            }
        }
Example #2
0
        /// <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();
        }