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

        /**
         * Show keys and values passed to the query string with GET
         */
        protected void DoGet(byte[] pdf, Stream stream)
        {
            // We get a resource from our web app
            PdfReader reader = new PdfReader(pdf);

            // Now we create the PDF
            using (PdfStamper stamper = new PdfStamper(reader, stream)) {
                // We add a submit button to the existing form
                PushbuttonField button = new PushbuttonField(
                    stamper.Writer, new Rectangle(90, 660, 140, 690), "submit"
                    );
                button.Text            = "POST";
                button.BackgroundColor = new GrayColor(0.7f);
                button.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                PdfFormField submit = button.Field;
                submit.Action = PdfAction.CreateSubmitForm(
                    WebContext.Request.RawUrl, null, PdfAction.SUBMIT_HTML_FORMAT
                    );
                stamper.AddAnnotation(submit, 1);
            }
        }
        private static byte[] AddSubmitButton(byte[] pdfFile)
        {
            var pr  = new PdfReader(pdfFile);
            var ims = new MemoryStream();
            var ps  = new PdfStamper(pr, ims);

            string url = AppSettings.URLForFormSubmission();

            PushbuttonField button2 = new PushbuttonField(ps.Writer, new Rectangle(100, 1000, 150, 1030), "Submit");

            button2.BackgroundColor = new GrayColor(0.7f);
            button2.Text            = "Submit";
            button2.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;

            PdfFormField submit2 = button2.Field;

            submit2.Action = PdfAction.CreateSubmitForm(url, null, PdfAction.SUBMIT_PDF);
            ps.AddAnnotation(submit2, 1);

            ps.Close();
            return(ims.ToArray());
        }
// ---------------------------------------------------------------------------

        /**
         * Show keys and values passed to the query string with GET
         */
        protected void DoGet(byte[] pdf, Stream stream)
        {
            // We get a resource from our web app
            PdfReader reader = new PdfReader(pdf);

            // Now we create the PDF
            using (PdfStamper stamper = new PdfStamper(reader, stream)) {
                // We add a submit button to the existing form
                PushbuttonField button = new PushbuttonField(
                    stamper.Writer, new Rectangle(90, 660, 140, 690), "submit"
                    );
                button.Text            = "POST";
                button.BackgroundColor = new GrayColor(0.7f);
                button.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                PdfFormField submit = button.Field;
                submit.Action = PdfAction.CreateSubmitForm(
                    WebContext.Request.RawUrl, null, 0
                    );
                stamper.AddAnnotation(submit, 1);
                // We add an extra field that can be used to upload a file
                TextField file = new TextField(
                    stamper.Writer, new Rectangle(160, 660, 470, 690), "image"
                    );
                file.Options         = TextField.FILE_SELECTION;
                file.BackgroundColor = new GrayColor(0.9f);
                PdfFormField upload = file.GetTextField();
                upload.SetAdditionalActions(PdfName.U,
                                            PdfAction.JavaScript(
                                                "this.getField('image').browseForFileToSubmit();"
                                                + "this.getField('submit').setFocus();",
                                                stamper.Writer
                                                )
                                            );
                stamper.AddAnnotation(upload, 1);
            }
        }
Exemple #5
0
// ---------------------------------------------------------------------------

        /**
         * Manipulates a PDF file src with the file dest as result
         * @param src the original PDF
         */
        // public byte[] ManipulatePdf(byte[] src) {
        public void ManipulatePdf(byte[] src, Stream stream)
        {
            string BaseUrl = Utility.GetServerBaseUrl();
            // create a reader
            PdfReader reader = new PdfReader(src);

            // create a stamper
            using (PdfStamper stamper = new PdfStamper(reader, stream)) {
                // create a submit button that posts the form as an HTML query string
                PushbuttonField button1 = new PushbuttonField(
                    stamper.Writer, new Rectangle(90, 660, 140, 690), "post"
                    );
                button1.Text            = "POST";
                button1.BackgroundColor = new GrayColor(0.7f);
                button1.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                PdfFormField submit1    = button1.Field;
                string       submit_url = new Uri(
                    new Uri(BaseUrl),
                    string.Format(
                        "/iTextInAction2Ed/WebHandler.ashx?{0}=Chapter09&{1}=ShowData",
                        Chapters.QS_CHAPTER, Chapters.QS_CLASS
                        )
                    ).ToString();
                submit1.Action = PdfAction.CreateSubmitForm(
                    submit_url, null,
                    PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES
                    );
                // add the button
                stamper.AddAnnotation(submit1, 1);
                // create a submit button that posts the form as FDF
                PushbuttonField button2 = new PushbuttonField(
                    stamper.Writer, new Rectangle(200, 660, 250, 690), "FDF"
                    );
                button2.BackgroundColor = new GrayColor(0.7f);
                button2.Text            = "FDF";
                button2.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                PdfFormField submit2 = button2.Field;
                submit2.Action = PdfAction.CreateSubmitForm(
                    submit_url, null, PdfAction.SUBMIT_EXCL_F_KEY
                    );
                // add the button
                stamper.AddAnnotation(submit2, 1);
                // create a submit button that posts the form as XFDF
                PushbuttonField button3 = new PushbuttonField(
                    stamper.Writer, new Rectangle(310, 660, 360, 690), "XFDF"
                    );
                button3.BackgroundColor = new GrayColor(0.7f);
                button3.Text            = "XFDF";
                button3.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                PdfFormField submit3 = button3.Field;
                submit3.Action = PdfAction.CreateSubmitForm(
                    submit_url, null, PdfAction.SUBMIT_XFDF
                    );
                // add the button
                stamper.AddAnnotation(submit3, 1);
                // create a reset button
                PushbuttonField button4 = new PushbuttonField(
                    stamper.Writer, new Rectangle(420, 660, 470, 690), "reset"
                    );
                button4.BackgroundColor = new GrayColor(0.7f);
                button4.Text            = "RESET";
                button4.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                PdfFormField reset = button4.Field;
                reset.Action = PdfAction.CreateResetForm(null, 0);
                // add the button
                stamper.AddAnnotation(reset, 1);
            }
        }
Exemple #6
0
// ---------------------------------------------------------------------------

        /**
         * Creates a PDF document.
         */
        public byte[] CreatePdf()
        {
            using (MemoryStream ms = new MemoryStream()) {
                using (Document document = new Document()) {
                    PdfWriter writer = PdfWriter.GetInstance(document, ms);
                    document.Open();
                    PdfFormField personal = PdfFormField.CreateEmpty(writer);
                    personal.FieldName = "personal";
                    PdfPTable table = new PdfPTable(3);
                    PdfPCell  cell;

                    table.AddCell("Your name:");
                    cell         = new PdfPCell();
                    cell.Colspan = 2;
                    TextField field = new TextField(writer, new Rectangle(0, 0), "name");
                    field.FontSize = 12;
                    cell.CellEvent = new ChildFieldEvent(
                        personal, field.GetTextField(), 1
                        );
                    table.AddCell(cell);
                    table.AddCell("Login:"******"loginname");
                    field.FontSize = 12;
                    cell.CellEvent = new ChildFieldEvent(
                        personal, field.GetTextField(), 1
                        );
                    table.AddCell(cell);
                    cell           = new PdfPCell();
                    field          = new TextField(writer, new Rectangle(0, 0), "password");
                    field.Options  = TextField.PASSWORD;
                    field.FontSize = 12;
                    cell.CellEvent = new ChildFieldEvent(
                        personal, field.GetTextField(), 1
                        );
                    table.AddCell(cell);
                    table.AddCell("Your motivation:");
                    cell             = new PdfPCell();
                    cell.Colspan     = 2;
                    cell.FixedHeight = 60;
                    field            = new TextField(writer, new Rectangle(0, 0), "reason");
                    field.Options    = TextField.MULTILINE;
                    field.FontSize   = 12;
                    cell.CellEvent   = new ChildFieldEvent(
                        personal, field.GetTextField(), 1
                        );
                    table.AddCell(cell);
                    document.Add(table);
                    writer.AddAnnotation(personal);

                    PushbuttonField button1 = new PushbuttonField(
                        writer, new Rectangle(90, 660, 140, 690), "post");
                    button1.Text            = "POST";
                    button1.BackgroundColor = new GrayColor(0.7f);
                    button1.Visibility      = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                    PdfFormField submit1 = button1.Field;
                    submit1.Action = PdfAction.CreateSubmitForm(
                        "/book/request", null,
                        PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES
                        );
                    writer.AddAnnotation(submit1);
                }
                return(ms.ToArray());
            }
        }