Example #1
0
        private static void AnnotationsAndFormFields(PDFFixedDocument document, PDFStructureElement seParent, PDFAnsiTrueTypeFont font)
        {
            PDFAnsiTrueTypeFont headingFont = new PDFAnsiTrueTypeFont(font, 16);
            PDFAnsiTrueTypeFont textFont    = new PDFAnsiTrueTypeFont(font, 10);
            PDFBrush            blackBrush  = new PDFBrush(PDFRgbColor.Black);
            PDFPen  blackPen = new PDFPen(PDFRgbColor.Black, 1);
            PDFPage page     = document.Pages.Add();

            page.TabOrder = PDFPageTabOrder.Structure;

            PDFStructureElement seSection = new PDFStructureElement(PDFStandardStructureTypes.Section);

            seSection.Title = "Annotations and form fields";
            seParent.AppendChild(seSection);

            PDFStructureElement seHeading = new PDFStructureElement(PDFStandardStructureTypes.Heading);

            seSection.AppendChild(seHeading);

            page.Graphics.BeginStructureElement(seHeading);
            page.Graphics.DrawString("Annotations and form fields", headingFont, blackBrush, 30, 50);
            page.Graphics.EndStructureElement();

            PDFStructureElement seParagraph1 = new PDFStructureElement(PDFStandardStructureTypes.Paragraph);

            seSection.AppendChild(seParagraph1);

            page.Graphics.BeginStructureElement(seParagraph1);
            page.Graphics.DrawString("Our website:", textFont, blackBrush, 30, 70);
            page.Graphics.EndStructureElement();

            PDFStructureElement seLink = new PDFStructureElement(PDFStandardStructureTypes.Link);

            seParagraph1.AppendChild(seLink);

            page.Graphics.BeginStructureElement(seLink);
            page.Graphics.DrawString("http://www.o2sol.com/", textFont, blackBrush, 100, 70);
            page.Graphics.EndStructureElement();

            PDFLinkAnnotation link = new PDFLinkAnnotation();

            page.Annotations.Add(link);
            link.DisplayRectangle = new PDFVisualRectangle(95, 70, 130, 10);
            link.HighlightStyle   = PDFLinkAnnotationHighlightStyle.Invert;
            link.Contents         = "http://www.o2sol.com/";

            PDFObjectReference linkRef = new PDFObjectReference();

            linkRef.Page   = page;
            linkRef.Stream = link.CosDictionary as PDFCosDictionaryContainer;
            seLink.AppendChild(linkRef);
            document.StructureTree.MapObjectReference(link, seLink);

            PDFStructureElement seParagraph2 = new PDFStructureElement(PDFStandardStructureTypes.Paragraph);

            seSection.AppendChild(seParagraph2);

            page.Graphics.BeginStructureElement(seParagraph2);
            page.Graphics.DrawString("Enter your name:", textFont, blackBrush, 30, 100);
            page.Graphics.EndStructureElement();

            PDFStructureElement seForm = new PDFStructureElement(PDFStandardStructureTypes.Form);

            seParagraph2.AppendChild(seForm);

            PDFTextBoxField fldName = new PDFTextBoxField("name");

            page.Fields.Add(fldName);
            fldName.AlternateName = "Enter your name";
            fldName.Widgets[0].DisplayRectangle = new PDFVisualRectangle(120, 95, 130, 20);
            fldName.Widgets[0].BorderColor      = PDFRgbColor.Blue;
            fldName.Widgets[0].BorderWidth      = 1;
            fldName.Widgets[0].BackgroundColor  = PDFRgbColor.LightBlue;
            fldName.Widgets[0].Font             = textFont;

            PDFObjectReference fieldRef = new PDFObjectReference();

            fieldRef.Page   = page;
            fieldRef.Stream = fldName.Widgets[0].CosDictionary as PDFCosDictionaryContainer;
            seForm.AppendChild(fieldRef);
            document.StructureTree.MapObjectReference(fldName.Widgets[0], seForm);
        }
Example #2
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run()
        {
            PDFFixedDocument document  = new PDFFixedDocument();
            PDFStandardFont  helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);
            PDFBrush         brush     = new PDFBrush();

            PDFPage page = document.Pages.Add();

            // First name
            page.Canvas.DrawString("First name:", helvetica, brush, 50, 50);
            PDFTextBoxField firstNameTextBox = new PDFTextBoxField("firstname");

            page.Fields.Add(firstNameTextBox);
            firstNameTextBox.Widgets[0].Font             = helvetica;
            firstNameTextBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 45, 200, 20);
            firstNameTextBox.Widgets[0].BorderColor      = PDFRgbColor.Black;
            firstNameTextBox.Widgets[0].BorderWidth      = 1;

            // Last name
            page.Canvas.DrawString("Last name:", helvetica, brush, 50, 80);
            PDFTextBoxField lastNameTextBox = new PDFTextBoxField("lastname");

            page.Fields.Add(lastNameTextBox);
            lastNameTextBox.Widgets[0].Font             = helvetica;
            lastNameTextBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 75, 200, 20);
            lastNameTextBox.Widgets[0].BorderColor      = PDFRgbColor.Black;
            lastNameTextBox.Widgets[0].BorderWidth      = 1;

            // Sex
            page.Canvas.DrawString("Sex:", helvetica, brush, 50, 110);
            PDFRadioButtonField  sexRadioButton = new PDFRadioButtonField("sex");
            PDFRadioButtonWidget maleRadioItem  = new PDFRadioButtonWidget();

            sexRadioButton.Widgets.Add(maleRadioItem);
            PDFRadioButtonWidget femaleRadioItem = new PDFRadioButtonWidget();

            sexRadioButton.Widgets.Add(femaleRadioItem);
            page.Fields.Add(sexRadioButton);

            page.Canvas.DrawString("Male", helvetica, brush, 180, 110);
            maleRadioItem.ExportValue      = "M";
            maleRadioItem.CheckStyle       = PDFCheckStyle.Circle;
            maleRadioItem.DisplayRectangle = new PDFDisplayRectangle(150, 105, 20, 20);
            maleRadioItem.BorderColor      = PDFRgbColor.Black;
            maleRadioItem.BorderWidth      = 1;

            page.Canvas.DrawString("Female", helvetica, brush, 280, 110);
            femaleRadioItem.ExportValue      = "F";
            femaleRadioItem.CheckStyle       = PDFCheckStyle.Circle;
            femaleRadioItem.DisplayRectangle = new PDFDisplayRectangle(250, 105, 20, 20);
            femaleRadioItem.BorderColor      = PDFRgbColor.Black;
            femaleRadioItem.BorderWidth      = 1;

            // First car
            page.Canvas.DrawString("First car:", helvetica, brush, 50, 140);
            PDFDropDownListField firstCarList = new PDFDropDownListField("firstcar");

            firstCarList.Items.Add(new PDFListItem("Mercedes", "Mercedes"));
            firstCarList.Items.Add(new PDFListItem("BMW", "BMW"));
            firstCarList.Items.Add(new PDFListItem("Audi", "Audi"));
            firstCarList.Items.Add(new PDFListItem("Volkswagen", "Volkswagen"));
            firstCarList.Items.Add(new PDFListItem("Porsche", "Porsche"));
            firstCarList.Items.Add(new PDFListItem("Honda", "Honda"));
            firstCarList.Items.Add(new PDFListItem("Toyota", "Toyota"));
            firstCarList.Items.Add(new PDFListItem("Lexus", "Lexus"));
            firstCarList.Items.Add(new PDFListItem("Infiniti", "Infiniti"));
            firstCarList.Items.Add(new PDFListItem("Acura", "Acura"));
            page.Fields.Add(firstCarList);
            firstCarList.Widgets[0].Font             = helvetica;
            firstCarList.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 135, 200, 20);
            firstCarList.Widgets[0].BorderColor      = PDFRgbColor.Black;
            firstCarList.Widgets[0].BorderWidth      = 1;

            // Second car
            page.Canvas.DrawString("Second car:", helvetica, brush, 50, 170);
            PDFListBoxField secondCarList = new PDFListBoxField("secondcar");

            secondCarList.Items.Add(new PDFListItem("Mercedes", "Mercedes"));
            secondCarList.Items.Add(new PDFListItem("BMW", "BMW"));
            secondCarList.Items.Add(new PDFListItem("Audi", "Audi"));
            secondCarList.Items.Add(new PDFListItem("Volkswagen", "Volkswagen"));
            secondCarList.Items.Add(new PDFListItem("Porsche", "Porsche"));
            secondCarList.Items.Add(new PDFListItem("Honda", "Honda"));
            secondCarList.Items.Add(new PDFListItem("Toyota", "Toyota"));
            secondCarList.Items.Add(new PDFListItem("Lexus", "Lexus"));
            secondCarList.Items.Add(new PDFListItem("Infiniti", "Infiniti"));
            secondCarList.Items.Add(new PDFListItem("Acura", "Acura"));
            page.Fields.Add(secondCarList);
            secondCarList.Widgets[0].Font             = helvetica;
            secondCarList.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 165, 200, 60);
            secondCarList.Widgets[0].BorderColor      = PDFRgbColor.Black;
            secondCarList.Widgets[0].BorderWidth      = 1;

            // I agree
            page.Canvas.DrawString("I agree:", helvetica, brush, 50, 240);
            PDFCheckBoxField agreeCheckBox = new PDFCheckBoxField("agree");

            page.Fields.Add(agreeCheckBox);
            agreeCheckBox.Widgets[0].Font = helvetica;
            (agreeCheckBox.Widgets[0] as PDFCheckWidget).ExportValue = "YES";
            (agreeCheckBox.Widgets[0] as PDFCheckWidget).CheckStyle  = PDFCheckStyle.Check;
            agreeCheckBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 235, 20, 20);
            agreeCheckBox.Widgets[0].BorderColor      = PDFRgbColor.Black;
            agreeCheckBox.Widgets[0].BorderWidth      = 1;

            // Sign here
            page.Canvas.DrawString("Sign here:", helvetica, brush, 50, 270);
            PDFSignatureField signHereField = new PDFSignatureField("signhere");

            page.Fields.Add(signHereField);
            signHereField.Widgets[0].Font             = helvetica;
            signHereField.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 265, 200, 60);

            // Submit form
            PDFPushButtonField submitBtn = new PDFPushButtonField("submit");

            page.Fields.Add(submitBtn);
            submitBtn.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(450, 45, 150, 30);
            (submitBtn.Widgets[0] as PDFPushButtonWidget).Caption = "Submit form";
            submitBtn.Widgets[0].BackgroundColor = PDFRgbColor.LightGray;
            PDFSubmitFormAction submitFormAction = new PDFSubmitFormAction();

            submitFormAction.DataFormat = PDFSubmitDataFormat.FDF;
            submitFormAction.Fields.Add("firstname");
            submitFormAction.Fields.Add("lastname");
            submitFormAction.Fields.Add("sex");
            submitFormAction.Fields.Add("firstcar");
            submitFormAction.Fields.Add("secondcar");
            submitFormAction.Fields.Add("agree");
            submitFormAction.Fields.Add("signhere");
            submitFormAction.SubmitFields = true;
            submitFormAction.Url          = "http://www.o2sol.com/";
            submitBtn.Widgets[0].MouseUp  = submitFormAction;

            // Reset form
            PDFPushButtonField resetBtn = new PDFPushButtonField("reset");

            page.Fields.Add(resetBtn);
            resetBtn.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(450, 85, 150, 30);
            (resetBtn.Widgets[0] as PDFPushButtonWidget).Caption = "Reset form";
            resetBtn.Widgets[0].BackgroundColor = PDFRgbColor.LightGray;
            PDFResetFormAction resetFormAction = new PDFResetFormAction();

            resetBtn.Widgets[0].MouseUp = resetFormAction;

            // Print form
            PDFPushButtonField printBtn = new PDFPushButtonField("print");

            page.Fields.Add(printBtn);
            printBtn.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(450, 125, 150, 30);
            (printBtn.Widgets[0] as PDFPushButtonWidget).Caption = "Print form";
            printBtn.Widgets[0].BackgroundColor = PDFRgbColor.LightGray;
            PDFJavaScriptAction printAction = new PDFJavaScriptAction();

            printAction.Script          = "this.print(true);\n";
            printBtn.Widgets[0].MouseUp = printAction;

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "formgenerator.pdf") };
            return(output);
        }