public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "NotoSansGurmukhi-Regular.ttf",
                                                     PdfEncodings.IDENTITY_H);

            // ਮੈਨੂੰ ਧੱਕੋ
            String line1 = "\u0A2E\u0A48\u0A28\u0A42\u0A70\u0020\u0A27\u0A71\u0A15\u0A4B";

            Rectangle rectangle = new Rectangle(50, 650, 100, 25);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rectangle, "Name", line1);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
Example #2
0
        public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            // Embedded parameter indicates whether the font is to be embedded into the target document.
            // We set it to make sure that the resultant document looks the same within different environments
            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "NotoSansTamil-Regular.ttf",
                                                     PdfEncodings.IDENTITY_H, true);

            // என்னை தள்ளி விடு
            String text =
                "\u0B8E\u0BA9\u0BCD\u0BA9\u0BC8\u0020\u0BA4\u0BB3\u0BCD\u0BB3\u0BBF\u0020\u0BB5\u0BBF\u0B9F\u0BC1";

            // Embed entire font without any subsetting. Please note that without subset it's impossible to edit a form field
            // with the predefined font
            font.SetSubset(false);
            Rectangle rect = new Rectangle(50, 650, 140, 30);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rect, "Name", text);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
Example #3
0
        public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "NotoSansThai-Regular.ttf",
                                                     PdfEncodings.IDENTITY_H);

            // Embed entire font without any subsetting. Please note that without subset it's impossible to edit a form field
            // with the predefined font
            font.SetSubset(false);

            // ผลักฉัน
            String text = "\u0E1C\u0E25\u0E31\u0E01\u0E09\u0E31\u0E19";

            Rectangle rect = new Rectangle(50, 650, 80, 25);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rect, "Name", text);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
        public virtual void PdfA1bButtonAppearanceRegenerateTest()
        {
            String          name         = "pdfA1b_ButtonAppearanceRegenerateTest";
            String          outPath      = destinationFolder + name + ".pdf";
            String          cmpPath      = cmpFolder + "cmp_" + name + ".pdf";
            String          diff         = "diff_" + name + "_";
            PdfWriter       writer       = new PdfWriter(outPath);
            Stream          @is          = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
            PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1"
                                                               , @is);
            PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);

            doc.SetTagged();
            doc.GetCatalog().SetLang(new PdfString("en-US"));
            doc.AddNewPage();
            PdfAcroForm  form   = PdfAcroForm.GetAcroForm(doc, true);
            Rectangle    rect   = new Rectangle(36, 626, 100, 40);
            PdfFont      font   = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
            PdfFormField button = PdfFormField.CreatePushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel
                                                                .PDF_A_1B);

            button.RegenerateField();
            form.AddField(button);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
                                                                             ));
        }
        public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "KhmerOS.ttf",
                                                     PdfEncodings.IDENTITY_H);

            // Embed entire font without any subsetting. Please note that without subset it's impossible to edit a form field
            // with the predefined font
            font.SetSubset(false);

            // រុញ​ខ្ញុំ
            String text = "\u179A\u17BB\u1789\u200B\u1781\u17D2\u1789\u17BB\u17C6";

            Rectangle rect = new Rectangle(50, 650, 100, 25);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rect, "Name", text);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
        public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "NotoNaskhArabic-Regular.ttf",
                                                     PdfEncodings.IDENTITY_H);

            // ادفعني
            String text = "\u0627\u062F\u0641\u0639\u0646\u064A";

            Rectangle rect = new Rectangle(50, 650, 100, 25);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rect, "Name", text);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
Example #7
0
        public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "NotoSansDevanagari-Regular.ttf",
                                                     PdfEncodings.IDENTITY_H);

            // पुश मे
            String text = "\u092A\u0941\u0936\u0020\u092E\u0947";

            Rectangle rect = new Rectangle(50, 650, 150, 25);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rect, "Name", text);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
 public override void Draw(DrawContext drawContext)
 {
     base.Draw(drawContext);
     if (!IsFlatten())
     {
         String    value    = GetDefaultValue();
         String    name     = GetModelId();
         UnitValue fontSize = (UnitValue)this.GetPropertyAsUnitValue(Property.FONT_SIZE);
         if (!fontSize.IsPointValue())
         {
             fontSize = UnitValue.CreatePointValue(DEFAULT_FONT_SIZE);
         }
         PdfDocument doc  = drawContext.GetDocument();
         Rectangle   area = GetOccupiedArea().GetBBox().Clone();
         ApplyMargins(area, false);
         PdfPage            page   = doc.GetPage(occupiedArea.GetPageNumber());
         PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, doc.GetDefaultFont(), fontSize
                                                                   .GetValue());
         button.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_NONE);
         button.SetBorderWidth(0);
         button.SetBackgroundColor(null);
         TransparentColor color = GetPropertyAsTransparentColor(Property.FONT_COLOR);
         if (color != null)
         {
             button.SetColor(color.GetColor());
         }
         PdfAcroForm forms = PdfAcroForm.GetAcroForm(doc, true);
         //Add fields only if it isn't already added. This can happen on split.
         if (forms.GetField(name) == null)
         {
             forms.AddField(button, page);
         }
     }
 }
        public virtual void ButtonFieldTest01()
        {
            String             filename = destinationFolder + "buttonFieldTest01.pdf";
            PdfWriter          writer   = new PdfWriter(new FileStream(filename, FileMode.Create));
            PdfDocument        pdfDoc   = new PdfDocument(writer);
            PdfAcroForm        form     = PdfAcroForm.GetAcroForm(pdfDoc, true);
            Rectangle          rect     = new Rectangle(36, 700, 20, 20);
            Rectangle          rect1    = new Rectangle(36, 680, 20, 20);
            PdfButtonFormField group    = PdfFormField.CreateRadioGroup(pdfDoc, "TestGroup", "1");

            PdfFormField.CreateRadioButton(pdfDoc, rect, group, "1");
            PdfFormField.CreateRadioButton(pdfDoc, rect1, group, "2");
            form.AddField(group);
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDoc, new Rectangle(36, 650, 40, 20), "push"
                                                                          , "Capcha");
            PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(36, 560, 20, 20), "TestCheck"
                                                                      , "1");

            form.AddField(pushButton);
            form.AddField(checkBox);
            pdfDoc.Close();
            CompareTool compareTool  = new CompareTool();
            String      errorMessage = compareTool.CompareByContent(filename, sourceFolder + "cmp_buttonFieldTest01.pdf", destinationFolder
                                                                    , "diff_");

            if (errorMessage != null)
            {
                NUnit.Framework.Assert.Fail(errorMessage);
            }
        }
        public virtual void CreatePDF(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

            PdfFont font = PdfFontFactory.CreateFont(FONTS_FOLDER + "NotoSansGujarati-Regular.ttf",
                                                     PdfEncodings.IDENTITY_H);

            // મને દબાણ કરો
            String text = "\u0AAE\u0AA8\u0AC7\u0020\u0AA6\u0AAC\u0ABE\u0AA3\u0020\u0A95\u0AB0\u0ACB";

            Rectangle rect = new Rectangle(50, 650, 100, 25);

            // Create a button for the form field, set its font and size
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDocument, rect, "Name", text);

            pushButton
            .SetFont(font)
            .SetFontSize(10);

            // Add the button to the form
            form.AddField(pushButton);

            pdfDocument.Close();
        }
Example #11
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.attach.impl.layout.form.renderer.AbstractFormFieldRenderer#applyAcroField(com.itextpdf.layout.renderer.DrawContext)
         */
        protected internal override void ApplyAcroField(DrawContext drawContext)
        {
            String    value    = GetDefaultValue();
            String    name     = GetModelId();
            UnitValue fontSize = (UnitValue)this.GetPropertyAsUnitValue(Property.FONT_SIZE);

            if (!fontSize.IsPointValue())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Impl.Layout.Form.Renderer.ButtonRenderer));
                logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Property
                                                      .FONT_SIZE));
            }
            PdfDocument doc  = drawContext.GetDocument();
            Rectangle   area = flatRenderer.GetOccupiedArea().GetBBox().Clone();

            ApplyPaddings(area, true);
            PdfPage            page   = doc.GetPage(occupiedArea.GetPageNumber());
            PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, font, fontSize.GetValue(
                                                                          ));
            Background background = this.GetProperty <Background>(Property.BACKGROUND);

            if (background != null && background.GetColor() != null)
            {
                button.SetBackgroundColor(background.GetColor());
            }
            ApplyDefaultFieldProperties(button);
            PdfAcroForm.GetAcroForm(doc, true).AddField(button, page);
        }
Example #12
0
        public static PdfAcroForm AddAcroForm(Document doc)
        {
            Paragraph title = new Paragraph("Application for employment").SetTextAlignment(TextAlignment.CENTER).SetFontSize
                                  (16);

            doc.Add(title);
            doc.Add(new Paragraph("Full name:").SetFontSize(12));
            doc.Add(new Paragraph("Native language:      English         French       German        Russian        Spanish"
                                  ).SetFontSize(12));
            doc.Add(new Paragraph("Experience in:       cooking        driving           software development").SetFontSize
                        (12));
            doc.Add(new Paragraph("Preferred working shift:").SetFontSize(12));
            doc.Add(new Paragraph("Additional information:").SetFontSize(12));
            //Add acroform
            PdfAcroForm form = PdfAcroForm.GetAcroForm(doc.GetPdfDocument(), true);
            //Create text field
            PdfTextFormField nameField = PdfTextFormField.CreateText(doc.GetPdfDocument(), new Rectangle(99, 753, 425,
                                                                                                         15), "name", "");

            form.AddField(nameField);
            //Create radio buttons
            PdfButtonFormField group = PdfFormField.CreateRadioGroup(doc.GetPdfDocument(), "language", "");

            PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(130, 728, 15, 15), group, "English");
            PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(200, 728, 15, 15), group, "French");
            PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(260, 728, 15, 15), group, "German");
            PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(330, 728, 15, 15), group, "Russian");
            PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(400, 728, 15, 15), group, "Spanish");
            form.AddField(group);
            //Create checkboxes
            for (int i = 0; i < 3; i++)
            {
                PdfButtonFormField checkField = PdfFormField.CreateCheckBox(doc.GetPdfDocument(), new Rectangle(119 + i *
                                                                                                                69, 701, 15, 15), String.Concat("experience", (i + 1).ToString()), "Off", PdfFormField.TYPE_CHECK);
                form.AddField(checkField);
            }
            //Create combobox
            String[]           options     = new String[] { "Any", "6.30 am - 2.30 pm", "1.30 pm - 9.30 pm" };
            PdfChoiceFormField choiceField = PdfFormField.CreateComboBox(doc.GetPdfDocument(), new Rectangle(163, 676,
                                                                                                             115, 15), "shift", "Any", options);

            form.AddField(choiceField);
            //Create multiline text field
            PdfTextFormField infoField = PdfTextFormField.CreateMultilineText(doc.GetPdfDocument(), new Rectangle(158,
                                                                                                                  625, 366, 40), "info", "");

            form.AddField(infoField);
            //Create push button field
            PdfButtonFormField button = PdfFormField.CreatePushButton(doc.GetPdfDocument(), new Rectangle(479, 594, 45
                                                                                                          , 15), "reset", "RESET");

            button.SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1", "experience2"
                                                                      , "experience3", "shift", "info" }, 0));
            form.AddField(button);
            return(form);
        }
 private void AddFormFieldsToDocument(PdfDocument pdfDoc, PdfAcroForm acroForm) {
     Rectangle rect = new Rectangle(36, 700, 20, 20);
     Rectangle rect1 = new Rectangle(36, 680, 20, 20);
     PdfButtonFormField group = PdfFormField.CreateRadioGroup(pdfDoc, "TestGroup", "1");
     PdfFormField.CreateRadioButton(pdfDoc, rect, group, "1");
     PdfFormField.CreateRadioButton(pdfDoc, rect1, group, "2");
     acroForm.AddField(group);
     PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDoc, new Rectangle(36, 650, 40, 20), "push"
         , "Capcha");
     PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(36, 560, 20, 20), "TestCheck"
         , "1");
     acroForm.AddField(pushButton);
     acroForm.AddField(checkBox);
 }
Example #14
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            PdfFont     font   = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            PdfAcroForm form   = PdfAcroForm.GetAcroForm(pdfDoc, true);

            // Define the position of a button that measures 108 by 26
            Rectangle          rect       = new Rectangle(36, 780, 108, 26);
            PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDoc, rect, "japanese",
                                                                          JAPANESE_TEXT, font, 12f);

            form.AddField(pushButton);

            pdfDoc.Close();
        }
Example #15
0
 public override void Draw(DrawContext drawContext)
 {
     base.Draw(drawContext);
     if (!IsFlatten())
     {
         String    value    = GetDefaultValue();
         String    name     = GetModelId();
         UnitValue fontSize = (UnitValue)this.GetPropertyAsUnitValue(Property.FONT_SIZE);
         if (!fontSize.IsPointValue())
         {
             fontSize = UnitValue.CreatePointValue(DEFAULT_FONT_SIZE);
         }
         PdfDocument doc  = drawContext.GetDocument();
         Rectangle   area = GetOccupiedArea().GetBBox().Clone();
         ApplyMargins(area, false);
         PdfPage            page   = doc.GetPage(occupiedArea.GetPageNumber());
         PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, doc.GetDefaultFont(), fontSize
                                                                   .GetValue());
         button.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_NONE);
         button.SetBorderWidth(0);
         button.SetBackgroundColor(null);
         TransparentColor color = GetPropertyAsTransparentColor(Property.FONT_COLOR);
         if (color != null)
         {
             button.SetColor(color.GetColor());
         }
         PdfAcroForm forms = PdfAcroForm.GetAcroForm(doc, true);
         //Add fields only if it isn't already added. This can happen on split.
         if (forms.GetField(name) == null)
         {
             forms.AddField(button, page);
         }
         if (doc.IsTagged())
         {
             TagTreePointer formParentPointer = doc.GetTagStructureContext().GetAutoTaggingPointer();
             IList <String> kidsRoles         = formParentPointer.GetKidsRoles();
             int            lastFormIndex     = kidsRoles.LastIndexOf(StandardRoles.FORM);
             TagTreePointer formPointer       = formParentPointer.MoveToKid(lastFormIndex);
             String         lang = this.GetProperty <String>(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE);
             if (lang != null)
             {
                 formPointer.GetProperties().SetLanguage(lang);
             }
             formParentPointer.MoveToParent();
         }
     }
 }
 public virtual void FormFieldTaggingTest07() {
     String outFileName = destinationFolder + "taggedPdfWithForms07.pdf";
     String cmpFileName = sourceFolder + "cmp_taggedPdfWithForms07.pdf";
     PdfWriter writer = new PdfWriter(outFileName);
     PdfReader reader = new PdfReader(sourceFolder + "taggedDocWithFields.pdf");
     PdfDocument pdfDoc = new PdfDocument(reader, writer);
     // Original document is already tagged, so there is no need to mark it as tagged again
     //        pdfDoc.setTagged();
     PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDoc, true);
     PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDoc, new Rectangle(36, 650, 40, 20), "push"
         , "Capcha");
     TagTreePointer tagPointer = pdfDoc.GetTagStructureContext().GetAutoTaggingPointer();
     tagPointer.MoveToKid(StandardRoles.DIV);
     acroForm.AddField(pushButton);
     pdfDoc.Close();
     CompareOutput(outFileName, cmpFileName);
 }
        public virtual void PdfA1DocWithPdfA1PushButtonFieldTest()
        {
            String  name         = "pdfA1DocWithPdfA1PushButtonField";
            String  fileName     = destinationFolder + name + ".pdf";
            String  cmp          = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1PushButtonField.pdf";
            PdfFont fontFreeSans = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
            Stream  @is          = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
            PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
            PdfADocument         pdfDoc           = new PdfADocument(new PdfWriter(fileName), conformanceLevel, new PdfOutputIntent("Custom"
                                                                                                                                    , "", "http://www.color.org", "sRGB IEC61966-2.1", @is));
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);

            form.AddField(PdfFormField.CreatePushButton(pdfDoc, new Rectangle(36, 526, 100, 20), "push button", "Push"
                                                        , fontFreeSans, 12, conformanceLevel));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(fileName, cmp, destinationFolder));
            NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(fileName));
        }
Example #18
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form   = PdfAcroForm.GetAcroForm(pdfDoc, true);

            PdfButtonFormField button = PdfFormField.CreatePushButton(pdfDoc,
                                                                      new Rectangle(36, 700, 36, 30), "post", "POST");

            button.SetBackgroundColor(ColorConstants.GRAY);
            button.SetValue("POST");

            // The second parameter is optional, it declares which fields to include in the submission or which to exclude,
            // depending on the setting of the Include/Exclude flag.
            button.SetAction(PdfAction.CreateSubmitForm("http://itextpdf.com:8180/book/request", null,
                                                        PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
            button.SetVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
            form.AddField(button);

            pdfDoc.Close();
        }
Example #19
0
        // ---------------------------------------------------------------------------

        /**
         * Create a pushbutton for a key
         * @param writer the PdfWriter
         * @param rect the position of the key
         * @param btn the label for the key
         * @param script the script to be executed when the button is pushed
         */
        public void AddPushButton(PdfWriter writer, Rectangle rect,
                                  String btn, String script)
        {
            float        w          = rect.Width;
            float        h          = rect.Height;
            PdfFormField pushbutton = PdfFormField.CreatePushButton(writer);

            pushbutton.FieldName = "btn_" + btn;
            pushbutton.SetWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH);
            PdfContentByte cb = writer.DirectContent;

            pushbutton.SetAppearance(
                PdfAnnotation.APPEARANCE_NORMAL,
                CreateAppearance(cb, btn, BaseColor.GRAY, w, h)
                );
            pushbutton.SetAppearance(
                PdfAnnotation.APPEARANCE_ROLLOVER,
                CreateAppearance(cb, btn, BaseColor.RED, w, h)
                );
            pushbutton.SetAppearance(
                PdfAnnotation.APPEARANCE_DOWN,
                CreateAppearance(cb, btn, BaseColor.BLUE, w, h)
                );
            pushbutton.SetAdditionalActions(
                PdfName.U,
                PdfAction.JavaScript(script, writer)
                );
            pushbutton.SetAdditionalActions(
                PdfName.E, PdfAction.JavaScript(
                    "this.showMove('" + btn + "');", writer
                    )
                );
            pushbutton.SetAdditionalActions(
                PdfName.X, PdfAction.JavaScript(
                    "this.showMove(' ');", writer
                    )
                );
            writer.AddAnnotation(pushbutton);
        }
Example #20
0
        // ********************************************************************
        // Fct:     CreateAnnot
        //
        // Descr:   -
        //
        // Owner:   erst
        // ********************************************************************
        public static void CreateAnnot(String src, String dest)
        {
            try
            {
                // Initialize PDF document
                PdfDocument ScrPdf  = new PdfDocument(new PdfReader(src));
                PdfPage     SrcPage = null;


                PdfDocument destPdf = new PdfDocument(new PdfWriter(dest));
                PageSize    ps      = PageSize.A4;
                destPdf.SetDefaultPageSize(ps);

                string idx = String.Empty;


                for (int i = 1; i <= 3; i++)
                {
                    idx = i.ToString();
                    // Add a page from Source PDF
                    SrcPage = ScrPdf.GetPage(i);
                    destPdf.AddPage(SrcPage.CopyTo(destPdf));
                    // Add empty page
                    //destPdf.AddNewPage(PageSize.A4);

                    PdfPage page = destPdf.GetPage(i);

                    // add title
                    PdfFont   fontHelvetica = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
                    PdfCanvas canvas        = new PdfCanvas(page);
                    canvas.BeginText().SetFontAndSize(fontHelvetica, 8).MoveText(420, 770).ShowText("My Title " + idx).EndText();

                    //Add acroform
                    PdfAcroForm form = PdfAcroForm.GetAcroForm(destPdf, true);
                    //Create text field
                    PdfTextFormField title = PdfTextFormField.CreateText(destPdf, new Rectangle(420, 750, 60, 10), idx + "_Title", idx + ".Page", fontHelvetica, 6);
                    PdfTextFormField user1 = PdfTextFormField.CreateText(destPdf, new Rectangle(420, 740, 60, 10), idx + "_User", "enter User", fontHelvetica, 6);
                    PdfTextFormField date1 = PdfTextFormField.CreateText(destPdf, new Rectangle(420, 730, 60, 10), idx + "_Date", "enter date" + idx, fontHelvetica, 6);
                    form.AddField(title);
                    form.AddField(user1);
                    form.AddField(date1);

                    //// Create Button Check

                    PdfButtonFormField button = PdfFormField.CreatePushButton(destPdf, new Rectangle(420, 720, 19, 10), idx + "_BtnOK", "OK");
                    //button.SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1", "experience2", "experience3", "shift", "info" }, 0));
                    PdfAction action = new PdfAction();
                    PdfAction date   = PdfAction.CreateJavaScript("AFDate_FormatEx(\"yyyy-mm-dd\");");

                    //PdfAction date = PdfAction.CreateNamed
                    //action.Put(button.GetFormType(), PdfName.F);

                    button.SetAction(date);
                    form.AddField(button);
                }


                ScrPdf.Close();
                destPdf.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
        }
Example #21
0
        public Task WriteAnnotatedPdfAsync(string pdfDocumentPath, IEnumerable <IAnnotation> annotations, string filePath)
        {
            using (var reader = new PdfReader(pdfDocumentPath))
                using (var outFile = File.Open(filePath, FileMode.Create))
                    using (var writer = new PdfWriter(outFile))
                        using (var doc = new PdfDocument(reader, writer))
                        {
                            var acroForm = PdfAcroForm.GetAcroForm(doc, true);
                            foreach (var ann in annotations)
                            {
                                var targets = ann.SelectedTargets;
                                if (targets == null || targets.Count < 1)
                                {
                                    targets = ann.Subject.Appearances ?? throw new ArgumentException($"The subject word {ann.Subject.Text} for a given annotation has no appearances.");
                                }
                                foreach (var trg in targets)
                                {
                                    var page = doc.GetPage(trg.Parent.Index + 1);

                                    var font       = PdfFontFactory.CreateRegisteredFont(iText.IO.Font.Constants.StandardFonts.HELVETICA);
                                    var coord      = trg.GetPdfCoords();
                                    var buttonRect = new Rectangle(coord.Llx, coord.Lly, coord.width, coord.height);
                                    var txRect     = GetAnnotationRect(buttonRect, font, ann, trg);

                                    var textFieldName = Guid.NewGuid().ToString("n");
                                    var textField     = PdfFormField.CreateText(doc, txRect, textFieldName);
                                    textField.SetValue(ann.Content, font, FontSize);
                                    textField.SetColor(ColorConstants.DARK_GRAY);
                                    textField.SetBackgroundColor(ColorConstants.LIGHT_GRAY);
                                    textField.SetReadOnly(true);
                                    textField.SetMultiline(true);
                                    textField.SetVisibility(PdfFormField.HIDDEN);
                                    textField.SetBorderColor(ColorConstants.LIGHT_GRAY);
                                    textField.SetFieldFlags(4097);

                                    acroForm.AddField(textField, page);

                                    var enter = PdfAction.CreateHide(textFieldName, false);
                                    var exit  = PdfAction.CreateHide(textFieldName, true);

                                    var btn = PdfFormField.CreatePushButton(doc, buttonRect, Guid.NewGuid().ToString("n"),
                                                                            string.Empty);
                                    btn.SetBackgroundColor(null);
                                    btn.SetBorderWidth(0);
                                    btn.SetAdditionalAction(PdfName.E, enter);
                                    btn.SetAdditionalAction(PdfName.X, exit);
                                    acroForm.AddField(btn, page);

                                    var underline = PdfTextMarkupAnnotation.CreateUnderline(new Rectangle(buttonRect.GetX(), buttonRect.GetY() - 2, buttonRect.GetWidth(), 3),
                                                                                            new float[]
                                    {
                                        buttonRect.GetX() + buttonRect.GetWidth(), buttonRect.GetY() + buttonRect.GetHeight(),
                                        buttonRect.GetX(), buttonRect.GetY() + buttonRect.GetHeight(),
                                        buttonRect.GetX() + buttonRect.GetWidth(), buttonRect.GetY() - 2,
                                        buttonRect.GetX(), buttonRect.GetY() - 2,
                                    });
                                    underline.SetColor(ColorConstants.YELLOW);
                                    page.AddAnnotation(underline);
                                }
                            }
                        }
            return(Task.CompletedTask);
        }