Ejemplo n.º 1
0
        private void FillPdfForm(string originalPdfFile, string destPdfFile, PdfFormMap formMap, Dictionary <string, dynamic> dbValues)
        {
            using PdfReader reader = new PdfReader(originalPdfFile);
            using PdfWriter writer = new PdfWriter(destPdfFile);
            reader.SetUnethicalReading(true);

            using PdfDocument pdf = new PdfDocument(reader, writer, new StampingProperties().UseAppendMode());
            var form = PdfAcroForm.GetAcroForm(pdf, false);

            foreach (var fieldMap in formMap.FieldMaps)
            {
                PdfFormField formField = form.GetField(fieldMap.Name);
                if (formField == null || formField.IsReadOnly())
                {
                    continue;
                }

                //string[] states = formField.GetAppearanceStates();
                //string statesStr = string.Empty;
                //foreach (var state in states)
                //{
                //    statesStr += $"{state} | ";
                //}

                //if (!string.IsNullOrEmpty(statesStr))
                //    list.Add($"[{formField.GetType().Name}] {field} - {statesStr}");

                if (!MatchCondition(fieldMap.Condition, dbValues))
                {
                    continue;
                }

                string value = (string)ResolveDynamicValue(fieldMap.Value, dbValues);

                if (!string.IsNullOrEmpty(value))
                {
                    if (formField is PdfButtonFormField buttonFormField)
                    {
                        formField = buttonFormField.SetCheckType(PdfFormField.TYPE_CHECK);
                        if (buttonFormField.IsRadio())
                        {
                            formField = buttonFormField.SetRadio(true);
                        }
                    }
                    formField.SetValue(value);
                }
            }
        }
        public ActionResult <PdfScanResult> Get(string pdfCode)
        {
            if (string.IsNullOrEmpty(pdfCode))
            {
                return(BadRequest());
            }

            var templateFilePath = $"{pdfCode}.pdf";

            if (!string.IsNullOrEmpty(_pdfFilesOptions.TemplatePath))
            {
                templateFilePath = $"{_pdfFilesOptions.TemplatePath}{Path.DirectorySeparatorChar}{templateFilePath}";
            }

            if (!System.IO.File.Exists(templateFilePath))
            {
                return(NotFound());
            }

            using PdfReader reader = new PdfReader(templateFilePath);
            reader.SetUnethicalReading(true);
            using PdfDocument pdf = new PdfDocument(reader);

            var scanPages = new PdfScanPage[pdf.GetNumberOfPages()];
            var result    = new PdfScanResult {
                PdfCode = pdfCode,
                Pages   = new List <PdfScanPage>(pdf.GetNumberOfPages())
            };

            //PdfPage pdfPage = pdf.GetPage(1);
            //PdfFormXObject pdfPageCopy = pdfPage.CopyAsFormXObject(pdf);
            //Image pageImage = new Image(pdfPageCopy);
            //ImageData b = new ImageData();
            //PdfImageXObject a = new PdfImageXObject()

            var form   = PdfAcroForm.GetAcroForm(pdf, false);
            var fields = form.GetFormFields();

            foreach (string field in fields.Keys)
            {
                PdfFormField formField = form.GetField(field);
                if (formField.IsReadOnly())
                {
                    continue;
                }

                var pdfValue = formField.GetValue();
                if (formField.GetWidgets() == null)
                {
                    continue;
                }
                if (formField.GetWidgets().Count <= 0)
                {
                    continue;
                }

                bool isButton = (formField is PdfButtonFormField);
                bool isText   = (formField is PdfTextFormField);

                if (!isText && !isButton)
                {
                    continue;
                }

                var page       = formField.GetWidgets().First().GetPage();
                var pageNumber = pdf.GetPageNumber(page);

                PdfScanPage scanPage = scanPages[pageNumber - 1];
                if (scanPage == null)
                {
                    scanPage = new PdfScanPage {
                        Number     = pageNumber,
                        FormFields = new List <PdfScanFormField>()
                    };
                }

                double minLowerLeftX  = double.MaxValue;
                double minLowerLeftY  = double.MaxValue;
                double maxUpperRightX = 0;
                double maxUpperRightY = 0;
                foreach (var widget in formField.GetWidgets())
                {
                    var rectangle = widget.GetRectangle();
                    if (rectangle == null)
                    {
                        continue;
                    }
                    if (rectangle.Size() < 4)
                    {
                        continue;
                    }

                    // API links explanations:
                    //https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-find-absolute-position-and-dimension-field
                    //https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-show-image-text-field-position
                    var lowerLeftX  = rectangle.GetAsNumber(0).GetValue(); //lower left position x
                    var lowerLeftY  = rectangle.GetAsNumber(1).GetValue(); //lower left position y
                    var upperRightX = rectangle.GetAsNumber(2).GetValue(); //upper right position x
                    var upperRightY = rectangle.GetAsNumber(3).GetValue(); //upper right position y

                    if (minLowerLeftX > lowerLeftX)
                    {
                        minLowerLeftX = lowerLeftX;
                    }
                    if (minLowerLeftY > lowerLeftY)
                    {
                        minLowerLeftY = lowerLeftY;
                    }

                    if (maxUpperRightX < upperRightX)
                    {
                        maxUpperRightX = upperRightX;
                    }
                    if (maxUpperRightY < upperRightY)
                    {
                        maxUpperRightY = upperRightY;
                    }
                }

                var scanFormField = new PdfScanFormField
                {
                    Name     = field,
                    Position = new Position
                    {
                        X = minLowerLeftX,
                        Y = minLowerLeftY
                    },
                    Size = new Size
                    {
                        Width  = maxUpperRightX - minLowerLeftX,
                        Height = maxUpperRightY - minLowerLeftY
                    }
                };

                scanPage.FormFields.Add(scanFormField);
                scanPages[pageNumber - 1] = scanPage;
            }

            result.Pages = scanPages.Where(sp => sp != null).ToList();
            return(Ok(result));
        }