コード例 #1
0
        private void goButton_Click(object sender, EventArgs e)
        {
            //TestPDF();  //find out whether acroform will work correctly

            //open file
            PdfDocument pdf = PdfReader.Open(@"YOURFILEPATHandNAME", PdfDocumentOpenMode.Modify);

            //fix some odd setting where filled fields don't always show                   SetupPDF(pdf);

            //find and fill fields
            PdfTextField txtEmployerName = (PdfTextField)(pdf.AcroForm.Fields["txtEmployerName"]);

            txtEmployerName.Value = new PdfString("My Name");

            PdfTextField txtEmployeeTitle = (PdfTextField)(pdf.AcroForm.Fields["txtEmployeeTitle"]);

            txtEmployeeTitle.Value = new PdfString("Bossin'");

            PdfCheckBoxField chxAttached = (PdfCheckBoxField)(pdf.AcroForm.Fields["chxAttached"]);

            chxAttached.Checked = true;

            //save file
            pdf.Save(@"NEWFILEPATHandNAMEHERE");
        }
コード例 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(@"..\..\..\..\..\..\Data\SampleB_1.pdf");

            doc.AllowCreateForm = true;

            //Create checkbox
            PdfCheckBoxField checkboxField  = new PdfCheckBoxField(doc.Pages[0], "fieldID");
            float            checkboxWidth  = 40;
            float            checkboxHeight = 40;

            checkboxField.Bounds      = new RectangleF(60, 300, checkboxWidth, checkboxHeight);
            checkboxField.BorderWidth = 0.75f;
            checkboxField.Checked     = true;
            checkboxField.Style       = PdfCheckBoxStyle.Cross;
            checkboxField.Required    = true;

            //Add in form
            doc.Form.Fields.Add(checkboxField);

            String result = "AddCheckBox-result.pdf";

            //Save to file
            doc.SaveToFile(result);

            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
コード例 #3
0
ファイル: Creator.cs プロジェクト: banzaiiiiii/PdfCreator
        // funktioniert nicht
        //public void getAndChangeFieldName()
        //{
        //    PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
        //    for(int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
        //    {
        //        PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
        //        string fieldName = field.Name;
        //    }

        //    List list = formWidget.FieldsWidget.GetFieldsByExportValue("Besichtigungsdatum");
        //    for(int i = 0; i < list.Count; i++)
        //    {
        //        string fieldName = list[i].Name;
        //        Console.WriteLine(fieldName);
        //    }
        //}


        public void createCheckbox(string fieldName, float koordinateXChange, float koordianteYChange)
        {
            PdfCheckBoxField checkbox       = new PdfCheckBoxField(page, fieldName);
            float            checkboxWidth  = 6;
            float            checkboxHeight = checkboxWidth;

            checkbox.Bounds      = new RectangleF(koordinateXChange, koordianteYChange, checkboxWidth, checkboxHeight);
            checkbox.BorderWidth = 1;
            //checkbox.BorderStyle = PdfBorderStyle.Solid;
            checkbox.Style = PdfCheckBoxStyle.Check;
            pdf.Form.Fields.Add(checkbox);
        }
コード例 #4
0
        /// <summary>
        /// Creates check box and adds it to the form.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="name"></param>
        /// <param name="tooltip"></param>
        private void CreateCheckBox(PdfPage page, string name, string tooltip)
        {
            // Create a Check box field.
            PdfCheckBoxField chb = new PdfCheckBoxField(page, name);

            // Set check box properties.
            chb.Font        = pdfFont;
            chb.ToolTip     = tooltip;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(System.Drawing.Color.FromArgb(255, 128, 128, 128));
            bounds.X       += chb.Bounds.Height + 10;

            document.Form.Fields.Add(chb);
        }
コード例 #5
0
        static void Main(string[] args)
        {
            var path = @"C:\Templates_POC\template.pdf";

            var doc = PdfReader.Open(path, PdfDocumentOpenMode.Modify);

            if (doc.AcroForm == null)
            {
                Console.WriteLine("Error, PDF Form Not Found!");
                return;
            }

            PdfAcroForm form = doc.AcroForm;

            if (form.Elements.ContainsKey("/NeedAppearances"))
            {
                form.Elements["/NeedAppearances"] = new PdfBoolean(true);
            }
            else
            {
                form.Elements.Add("/NeedAppearances", new PdfBoolean(true));
            }

            //Create fields
            PdfTextField     merchantPorfileOwnerField = (PdfTextField)(form.Fields["merchant_profile_owners_email_2"]);
            PdfCheckBoxField checkField = (PdfCheckBoxField)(form.Fields["seasson_Mar"]);

            merchantPorfileOwnerField.ReadOnly = false;
            checkField.ReadOnly             = false;
            checkField.Checked              = true;
            merchantPorfileOwnerField.Value = new PdfString("TEST ON C#");

            doc.Save(@"C:\Templates_POC\newcopy.pdf");

            Console.WriteLine("Hello World! " + merchantPorfileOwnerField.Text);
        }
コード例 #6
0
ファイル: Window1.xaml.cs プロジェクト: silexcorp/wpf-demos
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Create a new documenFt class object.
            pdfDoc = new PdfDocument();
            pdfDoc.ViewerPreferences.HideMenubar  = true;
            pdfDoc.ViewerPreferences.HideWindowUI = true;
            pdfDoc.ViewerPreferences.HideToolbar  = true;
            pdfDoc.ViewerPreferences.FitWindow    = true;

            pdfDoc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage;
            pdfDoc.PageSettings.Orientation     = PdfPageOrientation.Portrait;
            pdfDoc.PageSettings.Margins.All     = 0;

            //To set coordinates to draw form fields
            RectangleF       bounds = new RectangleF(180, 65, 156, 15);
            PdfUnitConvertor con    = new PdfUnitConvertor();

            PdfImage img = new PdfBitmap(@"..\..\..\..\..\..\..\Common\Images\PDF\Careers.png");

            //Set the page size
            SizeF pageSize = new SizeF(500, 310);

            pdfDoc.PageSettings.Height = pageSize.Height;
            pdfDoc.PageSettings.Width  = pageSize.Width;

            Font    f       = new Font("Calibri", 12f, System.Drawing.FontStyle.Bold);
            PdfFont pdfFont = new PdfTrueTypeFont(f);

            #region First Page
            pdfDoc.Pages.Add();

            PdfPage firstPage = pdfDoc.Pages[0];
            pdfDoc.Pages[0].Graphics.DrawImage(img, 0, 0, pageSize.Width, pageSize.Height);
            pdfDoc.Pages[0].Graphics.DrawString("General Information", pdfFont, new PdfSolidBrush(new PdfColor(213, 123, 19)), 25, 40);
            pdfDoc.Pages[0].Graphics.DrawString("Education Grade", pdfFont, new PdfSolidBrush(new PdfColor(213, 123, 19)), 25, 190);

            f       = new Font("Calibri", 10f);
            pdfFont = new PdfTrueTypeFont(f);
            //Create fields in first page.
            pdfDoc.Pages[0].Graphics.DrawString("First Name:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 65);

            //Create text box for firstname.
            CreateTextBox(pdfDoc.Pages[0], "FirstName", "First Name", f, bounds);

            pdfFont = new PdfTrueTypeFont(f);
            pdfDoc.Pages[0].Graphics.DrawString("Last Name:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 83);

            //Set position to draw form fields
            bounds.Y = bounds.Y + 18;
            //Create text box for lastname.
            CreateTextBox(pdfDoc.Pages[0], "LastName", "Last Name", f, bounds);

            pdfFont = new PdfTrueTypeFont(f);
            pdfDoc.Pages[0].Graphics.DrawString("Email:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 103);

            //Set position to draw form fields
            bounds.Y = bounds.Y + 18;

            //Create text box for Email.
            CreateTextBox(pdfDoc.Pages[0], "Email", "Email id", f, bounds);

            pdfFont = new PdfTrueTypeFont(f);
            pdfDoc.Pages[0].Graphics.DrawString("Business Phone:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 123);

            //Set position to draw form fields
            bounds.Y = bounds.Y + 18;

            //Create text box for Business phone.
            CreateTextBox(pdfDoc.Pages[0], "Business", "Business phone", f, bounds);

            pdfFont = new PdfTrueTypeFont(f);
            pdfDoc.Pages[0].Graphics.DrawString("Which position are\nyou applying for?", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 143);

            //Create combo box for Position.
            #region Create ComboBox
            //Set position to draw Combo Box
            bounds.Y = bounds.Y + 24;

            PdfComboBoxField comboBox = new PdfComboBoxField(pdfDoc.Pages[0], "JobTitle");
            comboBox.Bounds      = bounds;
            comboBox.BorderWidth = 1;
            comboBox.BorderColor = new PdfColor(System.Drawing.Color.Gray);

            pdfFont          = new PdfTrueTypeFont(f);
            comboBox.Font    = pdfFont;
            comboBox.ToolTip = "Job Title";


            comboBox.Items.Add(new PdfListFieldItem("Development", "accounts"));
            comboBox.Items.Add(new PdfListFieldItem("Support", "advertise"));
            comboBox.Items.Add(new PdfListFieldItem("Documentation", "agri"));

            pdfDoc.Form.Fields.Add(comboBox);
            #endregion

            pdfDoc.Pages[0].Graphics.DrawString("Highest qualification", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 217);

            //Create Checkbox box.
            #region Create CheckBox

            f       = new Font("Calibri", 8f);
            pdfFont = new PdfTrueTypeFont(f);;
            //Set position to draw Checkbox
            bounds.Y     = 239;
            bounds.X     = 25;
            bounds.Width = 10;

            bounds.Height = 10;

            // Create a Check Box
            PdfCheckBoxField chb = new PdfCheckBoxField(pdfDoc.Pages[0], "Adegree");

            chb.Font        = pdfFont;
            chb.ToolTip     = "degree";
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(System.Drawing.Color.Gray);
            bounds.X       += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("Associate degree", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);
            bounds.X += 90;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb             = new PdfCheckBoxField(pdfDoc.Pages[0], "Bdegree");
            chb.Font        = pdfFont;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(System.Drawing.Color.Gray);
            bounds.X       += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("Bachelor degree", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            bounds.X += 90;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb = new PdfCheckBoxField(pdfDoc.Pages[0], "college");

            chb.Font        = pdfFont;
            chb.ToolTip     = "college";
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(System.Drawing.Color.Gray);

            bounds.X += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("College", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            bounds.Y += 20;
            bounds.X  = 25;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb = new PdfCheckBoxField(pdfDoc.Pages[0], "pg");

            chb.Font        = pdfFont;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(System.Drawing.Color.Gray);
            bounds.X       += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("Post Graduate", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            bounds.X += 90;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb = new PdfCheckBoxField(pdfDoc.Pages[0], "mba");

            chb.Font        = pdfFont;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(System.Drawing.Color.Gray);

            bounds.X += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("MBA", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            pdfDoc.Form.Fields.Add(chb);
            #endregion

            # region Create Button
コード例 #7
0
        /// <summary>
        /// Create a simple PDF document
        /// </summary>
        /// <returns>Return the created PDF document as stream</returns>
        public MemoryStream JobApplicationPDF()
        {
            //Create a new PDF document
            PdfDocument pdfDoc = new PdfDocument();

            pdfDoc.ViewerPreferences.HideMenubar  = true;
            pdfDoc.ViewerPreferences.HideWindowUI = true;
            pdfDoc.ViewerPreferences.HideToolbar  = true;
            pdfDoc.ViewerPreferences.FitWindow    = true;

            pdfDoc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage;
            pdfDoc.PageSettings.Orientation     = PdfPageOrientation.Portrait;
            pdfDoc.PageSettings.Margins.All     = 0;

            //To set coordinates to draw form fields
            RectangleF       bounds = new RectangleF(180, 65, 156, 15);
            PdfUnitConverter con    = new PdfUnitConverter();



            //Read the file
            FileStream file = new FileStream(ResolveApplicationImagePath("careers.png"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            PdfImage img = new PdfBitmap(file);

            //Set the page size
            SizeF pageSize = new SizeF(500, 310);

            pdfDoc.PageSettings.Height = pageSize.Height;
            pdfDoc.PageSettings.Width  = pageSize.Width;

            PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold);

            #region First Page
            pdfDoc.Pages.Add();

            PdfPage firstPage = pdfDoc.Pages[0];
            pdfDoc.Pages[0].Graphics.DrawImage(img, 0, 0, pageSize.Width, pageSize.Height);
            pdfDoc.Pages[0].Graphics.DrawString("General Information", pdfFont, new PdfSolidBrush(new PdfColor(213, 123, 19)), 25, 40);
            pdfDoc.Pages[0].Graphics.DrawString("Education Grade", pdfFont, new PdfSolidBrush(new PdfColor(213, 123, 19)), 25, 190);

            pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
            //Create fields in first page.
            pdfDoc.Pages[0].Graphics.DrawString("First Name:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 65);

            //Create text box for firstname.
            PdfTextBoxField textBoxField1 = new PdfTextBoxField(pdfDoc.Pages[0], "FirstName");
            textBoxField1.ToolTip = "First Name";
            PdfStandardFont font1 = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
            textBoxField1.Font        = font1;
            textBoxField1.BorderColor = new PdfColor(Color.Gray);
            textBoxField1.BorderStyle = PdfBorderStyle.Beveled;
            textBoxField1.Bounds      = bounds;
            pdfDoc.Form.Fields.Add(textBoxField1);

            pdfDoc.Pages[0].Graphics.DrawString("Last Name:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 83);

            //Set position to draw form fields
            bounds.Y = bounds.Y + 18;
            //Create text box for lastname.
            PdfTextBoxField textBoxField2 = new PdfTextBoxField(pdfDoc.Pages[0], "LastName");
            textBoxField2.ToolTip     = "Last Name";
            textBoxField2.Font        = font1;
            textBoxField2.BorderColor = new PdfColor(Color.Gray);
            textBoxField2.BorderStyle = PdfBorderStyle.Beveled;
            textBoxField2.Bounds      = bounds;
            pdfDoc.Form.Fields.Add(textBoxField2);

            pdfDoc.Pages[0].Graphics.DrawString("Email:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 103);

            //Set position to draw form fields
            bounds.Y = bounds.Y + 18;

            //Create text box for Email.
            PdfTextBoxField textBoxField3 = new PdfTextBoxField(pdfDoc.Pages[0], "Email");
            textBoxField3.ToolTip     = "Email id";
            textBoxField3.Font        = font1;
            textBoxField3.BorderColor = new PdfColor(Color.Gray);
            textBoxField3.BorderStyle = PdfBorderStyle.Beveled;
            textBoxField3.Bounds      = bounds;
            pdfDoc.Form.Fields.Add(textBoxField3);

            pdfDoc.Pages[0].Graphics.DrawString("Business Phone:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 123);

            //Set position to draw form fields
            bounds.Y = bounds.Y + 18;

            //Create text box for Business phone.
            PdfTextBoxField textBoxField4 = new PdfTextBoxField(pdfDoc.Pages[0], "Business");
            textBoxField4.ToolTip     = "Business phone";
            textBoxField4.Font        = font1;
            textBoxField4.BorderColor = new PdfColor(Color.Gray);
            textBoxField4.BorderStyle = PdfBorderStyle.Beveled;
            textBoxField4.Bounds      = bounds;
            pdfDoc.Form.Fields.Add(textBoxField4);

            pdfDoc.Pages[0].Graphics.DrawString("Which position are\nyou applying for?", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 143);

            //Create combo box for Position.
            #region Create ComboBox
            //Set position to draw Combo Box
            bounds.Y = bounds.Y + 24;

            PdfComboBoxField comboBox = new PdfComboBoxField(pdfDoc.Pages[0], "JobTitle");
            comboBox.Bounds      = bounds;
            comboBox.BorderWidth = 1;
            comboBox.BorderColor = new PdfColor(Color.Gray);
            comboBox.Font        = pdfFont;
            comboBox.ToolTip     = "Job Title";


            comboBox.Items.Add(new PdfListFieldItem("Development", "accounts"));
            comboBox.Items.Add(new PdfListFieldItem("Support", "advertise"));
            comboBox.Items.Add(new PdfListFieldItem("Documentation", "agri"));

            pdfDoc.Form.Fields.Add(comboBox);
            #endregion

            pdfDoc.Pages[0].Graphics.DrawString("Highest qualification", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 217);

            //Create Checkbox box.
            #region Create CheckBox
            pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
            //Set position to draw Checkbox
            bounds.Y     = 239;
            bounds.X     = 25;
            bounds.Width = 10;

            bounds.Height = 10;

            // Create a Check Box
            PdfCheckBoxField chb = new PdfCheckBoxField(pdfDoc.Pages[0], "Adegree");

            chb.Font        = pdfFont;
            chb.ToolTip     = "degree";
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(Color.Gray);
            bounds.X       += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("Associate degree", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);
            bounds.X += 90;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb             = new PdfCheckBoxField(pdfDoc.Pages[0], "Bdegree");
            chb.Font        = pdfFont;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(Color.Gray);
            bounds.X       += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("Bachelor degree", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            bounds.X += 90;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb = new PdfCheckBoxField(pdfDoc.Pages[0], "college");

            chb.Font        = pdfFont;
            chb.ToolTip     = "college";
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(Color.Gray);

            bounds.X += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("College", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            bounds.Y += 20;
            bounds.X  = 25;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb = new PdfCheckBoxField(pdfDoc.Pages[0], "pg");

            chb.Font        = pdfFont;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(Color.Gray);
            bounds.X       += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("Post Graduate", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            bounds.X += 90;
            pdfDoc.Form.Fields.Add(chb);
            //Create a Checkbox
            chb = new PdfCheckBoxField(pdfDoc.Pages[0], "mba");

            chb.Font        = pdfFont;
            chb.Bounds      = bounds;
            chb.BorderColor = new PdfColor(Color.Gray);

            bounds.X += chb.Bounds.Height + 10;

            pdfDoc.Pages[0].Graphics.DrawString("MBA", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y);

            pdfDoc.Form.Fields.Add(chb);
            #endregion

            # region Create Button
コード例 #8
0
        private float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex)
        {
            float width   = page.Canvas.ClientSize.Width;
            float padding = 2;

            //measure field label
            String          label         = fieldNode.GetAttribute("label", "");
            PdfTrueTypeFont font1         = new PdfTrueTypeFont(new Font("Arial", 9f));
            PdfStringFormat format        = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            float           labelMaxWidth = width * 0.4f - 2 * padding;
            SizeF           labelSize     = font1.MeasureString(label, labelMaxWidth, format);

            //measure field height
            float fieldHeight = MeasureFieldHeight(fieldNode);

            float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight;

            height = height + 2;

            //draw background
            PdfBrush brush = PdfBrushes.SteelBlue;

            if (fieldIndex % 2 == 1)
            {
                brush = PdfBrushes.LightGreen;
            }
            page.Canvas.DrawRectangle(brush, 0, y, width, height);

            //draw field label
            PdfBrush   brush1      = PdfBrushes.LightYellow;
            RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height);

            page.Canvas.DrawString(label, font1, brush1, labelBounds, format);

            //daw field
            float  fieldMaxWidth = width * 0.57f - 2 * padding;
            float  fieldX        = labelBounds.Right + 2 * padding;
            float  fieldY        = y + (height - fieldHeight) / 2;
            String fieldType     = fieldNode.GetAttribute("type", "");
            String fieldId       = fieldNode.GetAttribute("id", "");
            bool   required      = "true" == fieldNode.GetAttribute("required", "");

            switch (fieldType)
            {
            case "text":
            case "password":
                PdfTextBoxField textField = new PdfTextBoxField(page, fieldId);
                textField.Bounds      = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                textField.BorderWidth = 0.75f;
                textField.BorderStyle = PdfBorderStyle.Solid;
                textField.Required    = required;
                if ("password" == fieldType)
                {
                    textField.Password = true;
                }
                if ("true" == fieldNode.GetAttribute("multiple", ""))
                {
                    textField.Multiline  = true;
                    textField.Scrollable = true;
                }
                form.Fields.Add(textField);
                break;

            case "checkbox":
                PdfCheckBoxField checkboxField  = new PdfCheckBoxField(page, fieldId);
                float            checkboxWidth  = fieldHeight - 2 * padding;
                float            checkboxHeight = checkboxWidth;
                checkboxField.Bounds      = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight);
                checkboxField.BorderWidth = 0.75f;
                checkboxField.Style       = PdfCheckBoxStyle.Cross;
                checkboxField.Required    = required;
                form.Fields.Add(checkboxField);
                break;

            case "list":
                XPathNodeIterator itemNodes = fieldNode.Select("item");
                if ("true" == fieldNode.GetAttribute("multiple", ""))
                {
                    PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId);
                    listBoxField.Bounds      = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                    listBoxField.BorderWidth = 0.75f;
                    listBoxField.MultiSelect = true;
                    listBoxField.Font        = new PdfFont(PdfFontFamily.Helvetica, 9f);
                    listBoxField.Required    = required;
                    //add items into list box.
                    foreach (XPathNavigator itemNode in itemNodes)
                    {
                        String text = itemNode.SelectSingleNode("text()").Value;
                        listBoxField.Items.Add(new PdfListFieldItem(text, text));
                    }
                    listBoxField.SelectedIndex = 0;
                    form.Fields.Add(listBoxField);

                    break;
                }
                if (itemNodes != null && itemNodes.Count <= 3)
                {
                    PdfRadioButtonListField radioButtonListFile
                        = new PdfRadioButtonListField(page, fieldId);
                    radioButtonListFile.Required = required;
                    //add items into radio button list.
                    float fieldItemHeight   = fieldHeight / itemNodes.Count;
                    float radioButtonWidth  = fieldItemHeight - 2 * padding;
                    float radioButtonHeight = radioButtonWidth;
                    foreach (XPathNavigator itemNode in itemNodes)
                    {
                        String text = itemNode.SelectSingleNode("text()").Value;
                        PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text);
                        fieldItem.BorderWidth = 0.75f;
                        fieldItem.Bounds      = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight);
                        radioButtonListFile.Items.Add(fieldItem);

                        float fieldItemLabelX    = fieldX + radioButtonWidth + padding;
                        SizeF fieldItemLabelSize = font1.MeasureString(text);
                        float fieldItemLabelY    = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2;
                        page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY);

                        fieldY = fieldY + fieldItemHeight;
                    }
                    form.Fields.Add(radioButtonListFile);

                    break;
                }

                //combo box
                PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId);
                comboBoxField.Bounds      = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                comboBoxField.BorderWidth = 0.75f;
                comboBoxField.Font        = new PdfFont(PdfFontFamily.Helvetica, 9f);
                comboBoxField.Required    = required;
                //add items into combo box.
                foreach (XPathNavigator itemNode in itemNodes)
                {
                    String text = itemNode.SelectSingleNode("text()").Value;
                    comboBoxField.Items.Add(new PdfListFieldItem(text, text));
                }
                form.Fields.Add(comboBoxField);
                break;
            }

            if (required)
            {
                //draw *
                float           flagX = width * 0.97f + padding;
                PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
                SizeF           size  = font3.MeasureString("*");
                float           flagY = y + (height - size.Height) / 2;
                page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY);
            }

            return(y + height);
        }
コード例 #9
0
ファイル: FormGenerator.cs プロジェクト: baitun/BGUEP-Journal
        /// <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.Graphics.DrawString("First name:", helvetica, brush, 50, 50);
            PdfTextBoxField firstNameTextBox = new PdfTextBoxField("firstname");
            page.Fields.Add(firstNameTextBox);
            firstNameTextBox.Widgets[0].Font = helvetica;
            firstNameTextBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 45, 200, 20);
            firstNameTextBox.Widgets[0].BorderColor = PdfRgbColor.Black;
            firstNameTextBox.Widgets[0].BorderWidth = 1;

            // Last name
            page.Graphics.DrawString("Last name:", helvetica, brush, 50, 80);
            PdfTextBoxField lastNameTextBox = new PdfTextBoxField("lastname");
            page.Fields.Add(lastNameTextBox);
            lastNameTextBox.Widgets[0].Font = helvetica;
            lastNameTextBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 75, 200, 20);
            lastNameTextBox.Widgets[0].BorderColor = PdfRgbColor.Black;
            lastNameTextBox.Widgets[0].BorderWidth = 1;

            // Sex
            page.Graphics.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.Graphics.DrawString("Male", helvetica, brush, 180, 110);
            maleRadioItem.ExportValue = "M";
            maleRadioItem.CheckStyle = PdfCheckStyle.Circle;
            maleRadioItem.VisualRectangle = new PdfVisualRectangle(150, 105, 20, 20);
            maleRadioItem.BorderColor = PdfRgbColor.Black;
            maleRadioItem.BorderWidth = 1;

            page.Graphics.DrawString("Female", helvetica, brush, 280, 110);
            femaleRadioItem.ExportValue = "F";
            femaleRadioItem.CheckStyle = PdfCheckStyle.Circle;
            femaleRadioItem.VisualRectangle = new PdfVisualRectangle(250, 105, 20, 20);
            femaleRadioItem.BorderColor = PdfRgbColor.Black;
            femaleRadioItem.BorderWidth = 1;

            // First car
            page.Graphics.DrawString("First car:", helvetica, brush, 50, 140);
            PdfComboBoxField firstCarList = new PdfComboBoxField("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].VisualRectangle = new PdfVisualRectangle(150, 135, 200, 20);
            firstCarList.Widgets[0].BorderColor = PdfRgbColor.Black;
            firstCarList.Widgets[0].BorderWidth = 1;

            // Second car
            page.Graphics.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].VisualRectangle = new PdfVisualRectangle(150, 165, 200, 60);
            secondCarList.Widgets[0].BorderColor = PdfRgbColor.Black;
            secondCarList.Widgets[0].BorderWidth = 1;

            // I agree
            page.Graphics.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].VisualRectangle = new PdfVisualRectangle(150, 235, 20, 20);
            agreeCheckBox.Widgets[0].BorderColor = PdfRgbColor.Black;
            agreeCheckBox.Widgets[0].BorderWidth = 1;

            // Sign here
            page.Graphics.DrawString("Sign here:", helvetica, brush, 50, 270);
            PdfSignatureField signHereField = new PdfSignatureField("signhere");
            page.Fields.Add(signHereField);
            signHereField.Widgets[0].Font = helvetica;
            signHereField.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 265, 200, 60);

            // Submit form
            PdfPushButtonField submitBtn = new PdfPushButtonField("submit");
            page.Fields.Add(submitBtn);
            submitBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(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.xfiniumsoft.com/";
            submitBtn.Widgets[0].MouseUp = submitFormAction;

            // Reset form
            PdfPushButtonField resetBtn = new PdfPushButtonField("reset");
            page.Fields.Add(resetBtn);
            resetBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(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].VisualRectangle = new PdfVisualRectangle(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, "xfinium.pdf.sample.formgenerator.pdf") };
            return output;
        }
コード例 #10
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.Graphics.DrawString("First name:", helvetica, brush, 50, 50);
            PdfTextBoxField firstNameTextBox = new PdfTextBoxField("firstname");

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

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

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

            // Sex
            page.Graphics.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.Graphics.DrawString("Male", helvetica, brush, 180, 110);
            maleRadioItem.ExportValue     = "M";
            maleRadioItem.CheckStyle      = PdfCheckStyle.Circle;
            maleRadioItem.VisualRectangle = new PdfVisualRectangle(150, 105, 20, 20);
            maleRadioItem.BorderColor     = PdfRgbColor.Black;
            maleRadioItem.BorderWidth     = 1;

            page.Graphics.DrawString("Female", helvetica, brush, 280, 110);
            femaleRadioItem.ExportValue     = "F";
            femaleRadioItem.CheckStyle      = PdfCheckStyle.Circle;
            femaleRadioItem.VisualRectangle = new PdfVisualRectangle(250, 105, 20, 20);
            femaleRadioItem.BorderColor     = PdfRgbColor.Black;
            femaleRadioItem.BorderWidth     = 1;

            // First car
            page.Graphics.DrawString("First car:", helvetica, brush, 50, 140);
            PdfComboBoxField firstCarList = new PdfComboBoxField("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].VisualRectangle = new PdfVisualRectangle(150, 135, 200, 20);
            firstCarList.Widgets[0].BorderColor     = PdfRgbColor.Black;
            firstCarList.Widgets[0].BorderWidth     = 1;

            // Second car
            page.Graphics.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].VisualRectangle = new PdfVisualRectangle(150, 165, 200, 60);
            secondCarList.Widgets[0].BorderColor     = PdfRgbColor.Black;
            secondCarList.Widgets[0].BorderWidth     = 1;

            // I agree
            page.Graphics.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].VisualRectangle = new PdfVisualRectangle(150, 235, 20, 20);
            agreeCheckBox.Widgets[0].BorderColor     = PdfRgbColor.Black;
            agreeCheckBox.Widgets[0].BorderWidth     = 1;

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

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

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

            page.Fields.Add(submitBtn);
            submitBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(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.xfiniumsoft.com/";
            submitBtn.Widgets[0].MouseUp  = submitFormAction;

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

            page.Fields.Add(resetBtn);
            resetBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(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].VisualRectangle = new PdfVisualRectangle(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, "xfinium.pdf.sample.formgenerator.pdf") };
            return(output);
        }
コード例 #11
0
ファイル: Form1.cs プロジェクト: e-iceblue/Spire.PDF-for-.NET
        private float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex)
        {
            float width = page.Canvas.ClientSize.Width;
            float padding = 2;

            //measure field label
            String label = fieldNode.GetAttribute("label", "");
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            float labelMaxWidth = width * 0.4f - 2 * padding;
            SizeF labelSize = font1.MeasureString(label, labelMaxWidth, format);

            //measure field height
            float fieldHeight = MeasureFieldHeight(fieldNode);

            float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight;
            height = height + 2;

            //draw background
            PdfBrush brush = PdfBrushes.SteelBlue;
            if (fieldIndex % 2 == 1)
            {
                brush = PdfBrushes.LightGreen;
            }
            page.Canvas.DrawRectangle(brush, 0, y, width, height);

            //draw field label
            PdfBrush brush1 = PdfBrushes.LightYellow;
            RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height);
            page.Canvas.DrawString(label, font1, brush1, labelBounds, format);

            //daw field
            float fieldMaxWidth = width * 0.57f - 2 * padding;
            float fieldX = labelBounds.Right + 2 * padding;
            float fieldY = y + (height - fieldHeight) / 2;
            String fieldType = fieldNode.GetAttribute("type", "");
            String fieldId = fieldNode.GetAttribute("id", "");
            bool required = "true" == fieldNode.GetAttribute("required", "");
            switch (fieldType)
            {
                case "text":
                case "password":
                    PdfTextBoxField textField = new PdfTextBoxField(page, fieldId);
                    textField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                    textField.BorderWidth = 0.75f;
                    textField.BorderStyle = PdfBorderStyle.Solid;
                    textField.Required = required;
                    if ("password" == fieldType)
                    {
                        textField.Password = true;
                    }
                    if ("true" == fieldNode.GetAttribute("multiple", ""))
                    {
                        textField.Multiline = true;
                        textField.Scrollable = true;
                    }
                    form.Fields.Add(textField);
                    break;
                case "checkbox":
                    PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, fieldId);
                    float checkboxWidth = fieldHeight - 2 * padding;
                    float checkboxHeight = checkboxWidth;
                    checkboxField.Bounds = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight);
                    checkboxField.BorderWidth = 0.75f;
                    checkboxField.Style = PdfCheckBoxStyle.Cross;
                    checkboxField.Required = required;
                    form.Fields.Add(checkboxField);
                    break;

                case "list":
                    XPathNodeIterator itemNodes = fieldNode.Select("item");
                    if ("true" == fieldNode.GetAttribute("multiple", ""))
                    {
                        PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId);
                        listBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                        listBoxField.BorderWidth = 0.75f;
                        listBoxField.MultiSelect = true;
                        listBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
                        listBoxField.Required = required;
                        //add items into list box.
                        foreach (XPathNavigator itemNode in itemNodes)
                        {
                            String text = itemNode.SelectSingleNode("text()").Value;
                            listBoxField.Items.Add(new PdfListFieldItem(text, text));
                        }
                        listBoxField.SelectedIndex = 0;
                        form.Fields.Add(listBoxField);

                        break;
                    }
                    if (itemNodes != null && itemNodes.Count <= 3)
                    {
                        PdfRadioButtonListField radioButtonListFile
                            = new PdfRadioButtonListField(page, fieldId);
                        radioButtonListFile.Required = required;
                        //add items into radio button list.
                        float fieldItemHeight = fieldHeight / itemNodes.Count;
                        float radioButtonWidth = fieldItemHeight - 2 * padding;
                        float radioButtonHeight = radioButtonWidth;
                        foreach (XPathNavigator itemNode in itemNodes)
                        {
                            String text = itemNode.SelectSingleNode("text()").Value;
                            PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text);
                            fieldItem.BorderWidth = 0.75f;
                            fieldItem.Bounds = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight);
                            radioButtonListFile.Items.Add(fieldItem);

                            float fieldItemLabelX = fieldX + radioButtonWidth + padding;
                            SizeF fieldItemLabelSize = font1.MeasureString(text);
                            float fieldItemLabelY = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2;
                            page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY);

                            fieldY = fieldY + fieldItemHeight;
                        }
                        form.Fields.Add(radioButtonListFile);

                        break;
                    }

                    //combo box
                    PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId);
                    comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                    comboBoxField.BorderWidth = 0.75f;
                    comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
                    comboBoxField.Required = required;
                    //add items into combo box.
                    foreach (XPathNavigator itemNode in itemNodes)
                    {
                        String text = itemNode.SelectSingleNode("text()").Value;
                        comboBoxField.Items.Add(new PdfListFieldItem(text, text));
                    }
                    form.Fields.Add(comboBoxField);
                    break;

            }

            if (required)
            {
                //draw *
                float flagX = width * 0.97f + padding;
                PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
                SizeF size = font3.MeasureString("*");
                float flagY = y + (height - size.Height) / 2;
                page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY);
            }

            return y + height;
        }
コード例 #12
0
 private static CompositeProperty asCompositeProperty(PdfCheckBoxField o)
 {
     return(new CompositeProperty(o.Name, typeof(bool)));
 }
コード例 #13
0
        public static bool AddFieldsToPDF(string fileName, string targetFileName, WordFieldsInfo wfi, ref bool keepGoing)
        {
            try {
                using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                    using (PdfLoadedDocument pdfDoc = new PdfLoadedDocument(stream))
                    {
                        if (pdfDoc.Form == null)
                        {
                            pdfDoc.CreateForm();
                        }
                        else
                        {
                            // Clear the existing Form
                            while (pdfDoc.Form.Fields.Count > 0)
                            {
                                pdfDoc.Form.Fields.RemoveAt(0);
                            }
                        }
                        foreach (WordFieldInfo fi in wfi.Fields)
                        {
                            if (!keepGoing)
                            {
                                return(false);
                            }
                            fi.Status = "Adding to PDF";
                            if (fi.Page <= pdfDoc.Pages.Count)
                            {
                                PdfPageBase page = pdfDoc.Pages[fi.Page - 1];
                                switch (fi.FieldType)
                                {
                                case FieldType.DropDownList:
                                    PdfComboBoxField cboField = new PdfComboBoxField(page, GetFieldName(fi));
                                    cboField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y,
                                                                                    (float)fi.Width, (float)fi.Height);
                                    foreach (KeyValuePair <string, string> kvp in fi.Options)
                                    {
                                        cboField.Items.Add(new PdfListFieldItem(kvp.Key, kvp.Value));
                                    }
                                    cboField.SelectedIndex = 0;
                                    pdfDoc.Form.Fields.Add(cboField);
                                    break;

                                case FieldType.SingleLine:
                                case FieldType.MultiLine:
                                    PdfTextBoxField txtField = new PdfTextBoxField(page, GetFieldName(fi));
                                    txtField.BackColor = new Syncfusion.Pdf.Graphics.PdfColor((float)0xdd);
                                    txtField.Bounds    = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y,
                                                                                       (float)fi.Width, (float)fi.Height);
                                    txtField.Multiline  = true;
                                    txtField.Scrollable = true;
                                    txtField.SpellCheck = true;
                                    pdfDoc.Form.Fields.Add(txtField);
                                    break;

                                case FieldType.Checkbox:
                                    PdfCheckBoxField chkField = new PdfCheckBoxField(page, GetFieldName(fi));
                                    chkField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y,
                                                                                    (float)fi.Width, (float)fi.Height);
                                    pdfDoc.Form.Fields.Add(chkField);
                                    break;

                                default:
                                    System.Diagnostics.Trace.WriteLine($"{fi.FieldType}");
                                    break;
                                }
                                fi.Status = "Added to PDF";
                            }
                            else
                            {
                                throw new IndexOutOfRangeException($"Field {fi.FieldType} at {fi.Page} ({fi.X}, {fi.Y}) is not on a valid page in the PDF");
                            }
                        }
                        using (Stream outStream = File.Open(targetFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                        {
                            pdfDoc.Save(outStream);
                        }

                        pdfDoc.Close(true);
                    }
                return(true);
            }
            catch (Exception ex)
            {
                throw;
            }
        }