Exemplo n.º 1
0
        public int GetAllignedFontSizeForMultiLines <T>(T detail, int defaultFontSize = 9)
        {
            IDictionary <String, PdfFormField> fields = _form.GetFormFields();

            Type type = detail.GetType();

            PropertyInfo[] properties     = type.GetProperties();
            int            targetFontsize = 0;

            foreach (PropertyInfo property in properties)
            {
                PdfFormField       toSet;
                PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>();

                if (fields.TryGetValue(property.Name, out toSet))
                {
                    var value = property.GetValue(detail, null);
                    if (!string.IsNullOrWhiteSpace(value?.ToString()))
                    {
                        if (required != null && required.Action.Equals(ActionFlag.MultiLines))
                        {
                            PdfFormField field    = fields[property.Name];
                            PdfArray     position = field.GetWidgets().First().GetRectangle();
                            float        width    = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue());
                            float        height   = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue());

                            PdfFontAttribute pdfFont = property.GetCustomAttribute <PdfFontAttribute>();
                            var font = pdfFont?.Type != null
                                ? PdfFontFactory.CreateRegisteredFont(pdfFont.Type)
                                : PdfFontFactory.CreateRegisteredFont(DefaultFont);

                            int latestTargetFontSize = GetFontSizeFittingMultiLinesInAcroForm(value.ToString(), width, height, font, defaultFontSize);
                            if (targetFontsize == 0)
                            {
                                targetFontsize = latestTargetFontSize;
                            }
                            else
                            {
                                targetFontsize = targetFontsize > latestTargetFontSize
                                    ? latestTargetFontSize
                                    : targetFontsize;
                            }
                        }
                    }
                }
            }
            return(targetFontsize);
        }
Exemplo n.º 2
0
        public void FillMultiLinesInAcroForm <T>(T detail, int fontSize)
        {
            IDictionary <String, PdfFormField> fields = _form.GetFormFields();

            Type type = detail.GetType();

            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                PdfFormField       toSet;
                PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>();

                if (fields.TryGetValue(property.Name, out toSet))
                {
                    var value = property.GetValue(detail, null);
                    if (!string.IsNullOrWhiteSpace(value?.ToString()))
                    {
                        if (required != null && required.Action.Equals(ActionFlag.MultiLines))
                        {
                            PdfFontAttribute pdfFont = property.GetCustomAttribute <PdfFontAttribute>();
                            var font = pdfFont?.Type != null
                                ? PdfFontFactory.CreateRegisteredFont(pdfFont.Type)
                                : PdfFontFactory.CreateRegisteredFont(DefaultFont);

                            toSet.SetValue(value.ToString()).SetFontAndSize(font, fontSize);

                            if (pdfFont != null)
                            {
                                toSet.SetColor(pdfFont.Color);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void FillImageInAcroForm <T>(T detail)
        {
            IDictionary <String, PdfFormField> fields = _form.GetFormFields();

            Type type = detail.GetType();

            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                PdfFormField       toSet;
                PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>();

                if (required != null &&
                    (required.Action.Equals(ActionFlag.Ignore) ||
                     required.Action.Equals(ActionFlag.MultiLines)))
                {
                    continue;
                }

                if (fields.TryGetValue(property.Name, out toSet))
                {
                    var          value = property.GetValue(detail, null);
                    PdfFormField field = fields[property.Name];
                    if (!string.IsNullOrWhiteSpace(value?.ToString()))
                    {
                        var widgets = field.GetWidgets();
                        if (widgets == null || widgets.Count == 0)
                        {
                            throw new ArgumentNullException($"no widgets to the field");
                        }

                        PdfArray position = widgets.First().GetRectangle();

                        PdfPage page = field.GetWidgets().First().GetPage();
                        if (page == null)
                        {
                            throw new ArgumentNullException(
                                      $"field widget annotation is not associated with any page");
                        }
                        int pageNum = _pdfDoc.GetPageNumber(page);

                        float width  = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue());
                        float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue());

                        Image image = new Image(ImageDataFactory.Create(File.ReadAllBytes(value.ToString())));
                        image.ScaleToFit(width, height);

                        float startX = (float)position.GetAsNumber(0).GetValue();
                        float startY = (float)position.GetAsNumber(1).GetValue();

                        PdfFontAttribute pdfFont = property.GetCustomAttribute <PdfFontAttribute>();
                        if (pdfFont != null)
                        {
                            ImagePositionAdjustification(pdfFont.Justification, width, height, image.GetImageScaledWidth(), image.GetImageScaledHeight(),
                                                         (float)position.GetAsNumber(0).GetValue(), (float)position.GetAsNumber(1).GetValue(), out startX, out startY);
                        }

                        image.SetFixedPosition(pageNum, startX, startY);
                        _form.RemoveField(field.GetFieldName().ToString());
                        _doc.Add(image);
                    }
                    else
                    {
                        if (required != null)
                        {
                            if (required.Action.Equals(ActionFlag.Required))
                            {
                                throw new ArgumentNullException($"No {property.Name} image found");
                            }
                            if (required.Action.Equals(ActionFlag.Optional))
                            {
                                _form.RemoveField(field.GetFieldName().ToString());
                                continue;
                            }
                            if (required.Action.Equals(ActionFlag.NotAvailable))
                            {
                                toSet.SetValue("N/A").SetFontSizeAutoScale().SetReadOnly(true);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void VerticalJustificationInAcroForm <T>(PdfFormField field, T detail, string propertyName, PdfFont pdfFont, PdfFontAttribute pdfFontAttribute)
        {
            var widgets = field.GetWidgets();

            if (widgets == null || widgets.Count == 0)
            {
                throw new ArgumentNullException($"no widgets to the field");
            }

            PdfArray position = widgets.First().GetRectangle();

            PdfPage page = field.GetWidgets().First().GetPage();

            if (page == null)
            {
                throw new ArgumentNullException(
                          $"field widget annotation is not associated with any page");
            }
            int pageNum = _pdfDoc.GetPageNumber(page);

            float width  = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue());
            float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue());
            float llx    = (float)position.GetAsNumber(0).GetValue();
            float lly    = (float)position.GetAsNumber(1).GetValue();
            float urx    = (float)position.GetAsNumber(2).GetValue();
            float ury    = (float)position.GetAsNumber(3).GetValue();

            Rectangle rect = new Rectangle(llx, lly, width, height);

            PdfCanvas canvas = new PdfCanvas(_pdfDoc, pageNum);

            canvas.SetLineWidth(0f).Rectangle(rect);

            string textValue = null;
            Type   type      = detail.GetType();

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (property.Name == propertyName)
                {
                    textValue = property.GetValue(detail, null).ToString();
                }
            }
            if (string.IsNullOrWhiteSpace(textValue))
            {
                return;
            }

            float verticalAdjustment = 0;
            var   text = new Text(textValue);

            if (pdfFont != null)
            {
                text.SetFont(pdfFont);
                text.SetFontColor(pdfFontAttribute.Color);
                if (pdfFontAttribute.Size != 0)
                {
                    text.SetFontSize(pdfFontAttribute.Size);
                }

                verticalAdjustment = (pdfFont.GetWidth(textValue, pdfFontAttribute.Size) / (urx - llx)) * pdfFont.GetAscent(textValue, pdfFontAttribute.Size);
            }

            Paragraph p = new Paragraph(text);

            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterLeft ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomLeft)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.LEFT);
            }
            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterMiddle ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomMiddle)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
            }
            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterRight ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomRight)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT);
            }

            // set line space
            p.SetMultipliedLeading(1);

            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterLeft ||
                pdfFontAttribute.Justification == PdfTextAlignment.CenterMiddle ||
                pdfFontAttribute.Justification == PdfTextAlignment.CenterRight)
            {
                new Canvas(canvas, _pdfDoc, rect)
                .Add(p.SetFixedPosition(llx, (ury + lly) / 2 - verticalAdjustment, urx - llx));
            }

            if (pdfFontAttribute.Justification == PdfTextAlignment.BottomLeft ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomMiddle ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomRight)
            {
                new Canvas(canvas, _pdfDoc, rect).Add(p);
            }

            _form.RemoveField(field.GetFieldName().ToString());
        }
Exemplo n.º 5
0
        public void FillTextInAcroForm <T>(T detail)
        {
            IDictionary <String, PdfFormField> fields = _form.GetFormFields();

            Type type = detail.GetType();

            PropertyInfo[] properties  = type.GetProperties();
            PdfFont        defaultFont = PdfFontFactory.CreateRegisteredFont(DefaultFont);

            foreach (PropertyInfo property in properties)
            {
                PdfFormField       toSet;
                PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>();

                if (required != null &&
                    (required.Action.Equals(ActionFlag.Ignore) ||
                     required.Action.Equals(ActionFlag.MultiLines)))
                {
                    continue;
                }

                if (fields.TryGetValue(property.Name, out toSet))
                {
                    var value = property.GetValue(detail, null);
                    if (!string.IsNullOrWhiteSpace(value?.ToString()))
                    {
                        toSet.SetValue(value.ToString()).SetReadOnly(true);

                        PdfFontAttribute pdfFont = property.GetCustomAttribute <PdfFontAttribute>();
                        if (pdfFont != null)
                        {
                            var font = pdfFont.Type != null
                                ? PdfFontFactory.CreateRegisteredFont(pdfFont.Type)
                                : defaultFont;

                            if (pdfFont.IsAutoScaleToBlock)
                            {
                                PdfArray position = toSet.GetWidgets().First().GetRectangle();
                                float    width    = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue());
                                float    height   = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue());

                                pdfFont.Size =
                                    GetFontSizeFittingMultiLinesInAcroForm(value.ToString(), width, height, font, pdfFont.Size != 0 ? pdfFont.Size : 12);
                            }

                            // default justification for form is from top left

                            if (pdfFont.Justification == PdfTextAlignment.CenterLeft ||
                                pdfFont.Justification == PdfTextAlignment.CenterMiddle ||
                                pdfFont.Justification == PdfTextAlignment.CenterRight ||
                                pdfFont.Justification == PdfTextAlignment.BottomLeft ||
                                pdfFont.Justification == PdfTextAlignment.BottomMiddle ||
                                pdfFont.Justification == PdfTextAlignment.BottomRight)
                            {
                                VerticalJustificationInAcroForm(toSet, detail, property.Name, font, pdfFont);
                                continue;
                            }

                            if (pdfFont.Justification == PdfTextAlignment.TopLeft ||
                                pdfFont.Justification == PdfTextAlignment.TopMiddle ||
                                pdfFont.Justification == PdfTextAlignment.TopRight)
                            {
                                toSet.SetJustification((int)pdfFont.Justification);
                            }

                            toSet.SetFont(font);

                            if (pdfFont.Size == 0)
                            {
                                toSet.SetFontSizeAutoScale();
                            }
                            else
                            {
                                toSet.SetFontSize(pdfFont.Size);
                            }

                            toSet.SetColor(pdfFont.Color);

                            if (!string.IsNullOrWhiteSpace(pdfFont.WarningCondition))
                            {
                                if (pdfFont.IsWarning(value.ToString(), pdfFont.WarningCondition))
                                {
                                    toSet.SetColor(Color.RED);
                                }
                            }
                        }
                        else
                        {
                            toSet.SetValue(value.ToString()).SetFont(defaultFont).SetFontSizeAutoScale().SetReadOnly(true);
                        }
                    }
                    else
                    {
                        if (required != null)
                        {
                            if (required.Action.Equals(ActionFlag.Required))
                            {
                                throw new ArgumentException($"{property.Name} is missing");
                            }
                            if (required.Action.Equals(ActionFlag.Optional))
                            {
                                continue;
                            }
                            if (required.Action.Equals(ActionFlag.NotAvailable))
                            {
                                toSet.SetValue("N/A").SetFontSizeAutoScale().SetReadOnly(true);
                            }
                        }
                    }
                }
            }
        }