Ejemplo 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);
        }
Ejemplo 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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo 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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void FillU3DImageInAcroForm <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 (fields.TryGetValue(property.Name, out toSet))
                {
                    var          value = property.GetValue(detail, null)?.ToString();
                    PdfFormField field = fields[property.Name];
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        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());

                        var u3DInfo = new U3DInfo
                        {
                            Height  = height,
                            Width   = width,
                            X       = (float)position.GetAsNumber(0).GetValue(),
                            Y       = (float)position.GetAsNumber(1).GetValue(),
                            Page    = pageNum,
                            U3DFile = value
                        };
                        _u3DInfos.Add(u3DInfo);

                        _form.RemoveField(field.GetFieldName().ToString());
                    }
                    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);
                            }
                        }
                    }
                }
            }

            // close the target pdf first:
            _pdfDoc?.Close();

            using (var helper = new U3DImageHelper(TargetFile))
            {
                foreach (var u3DInfo in _u3DInfos)
                {
                    helper.AddU3DImage(u3DInfo);
                }
            }

            // reopen the target pdf:
            _temporaryFile = Path.Combine(Path.GetDirectoryName(TargetFile), $"temp_{DateTime.Now.Ticks}.pdf");
            File.Move(TargetFile, _temporaryFile);
            Init(TargetFile, _temporaryFile);
        }
Ejemplo 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);
                            }
                        }
                    }
                }
            }
        }