Exemplo n.º 1
0
        public static void Run()
        {
            try
            {
                // ExStart:CreateDoc
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

                // Create a new document
                Document doc  = new Document();
                Page     page = doc.Pages.Add();
                // Add radio button field
                RadioButtonField field = new RadioButtonField(page);
                field.Rect        = new Aspose.Pdf.Rectangle(40, 650, 100, 720);
                field.PartialName = "NewField";

                // Add radio button options. please note that these options are situated
                // Neither horizontally nor vertically.
                // You can try to set any coordinates (and even size) for them.
                RadioButtonOptionField opt1 = new RadioButtonOptionField();
                opt1.Rect                   = new Aspose.Pdf.Rectangle(40, 650, 60, 670);
                opt1.OptionName             = "Item1";
                opt1.Border                 = new Border(opt1);
                opt1.Border.Width           = 1;
                opt1.Characteristics.Border = System.Drawing.Color.Black;

                RadioButtonOptionField opt2 = new RadioButtonOptionField();
                opt2.Rect                   = new Aspose.Pdf.Rectangle(60, 670, 80, 690);
                opt2.OptionName             = "Item2";
                opt2.Border                 = new Border(opt2);
                opt2.Border.Width           = 1;
                opt2.Characteristics.Border = System.Drawing.Color.Black;

                RadioButtonOptionField opt3 = new RadioButtonOptionField();
                opt3.Rect                   = new Aspose.Pdf.Rectangle(80, 690, 100, 710);
                opt3.OptionName             = "Item3";
                opt3.Border                 = new Border(opt3);
                opt3.Border.Width           = 1;
                opt3.Characteristics.Border = System.Drawing.Color.Black;
                field.Add(opt1);
                field.Add(opt2);
                field.Add(opt3);
                doc.Form.Add(field);

                dataDir = dataDir + "CreateDoc_out.pdf";
                // Save the PDF document
                doc.Save(dataDir);
                // ExEnd:CreateDoc
                Console.WriteLine("\nNew doc with 3 items radio button created successfully.\nFile saved at " + dataDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 2
0
        public static void Run()
        {
            try
            {
                // ExStart:CreateDoc
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

                // Create a new document
                Document doc = new Document();
                Page page = doc.Pages.Add();
                // Add radio button field
                RadioButtonField field = new RadioButtonField(page);
                field.Rect = new Aspose.Pdf.Rectangle(40, 650, 100, 720);
                field.PartialName = "NewField";

                // Add radio button options. please note that these options are situated 
                // Neither horizontally nor vertically. 
                // You can try to set any coordinates (and even size) for them. 
                RadioButtonOptionField opt1 = new RadioButtonOptionField();
                opt1.Rect = new Aspose.Pdf.Rectangle(40, 650, 60, 670);
                opt1.OptionName = "Item1";
                opt1.Border = new Border(opt1);
                opt1.Border.Width = 1;
                opt1.Characteristics.Border = System.Drawing.Color.Black;

                RadioButtonOptionField opt2 = new RadioButtonOptionField();
                opt2.Rect = new Aspose.Pdf.Rectangle(60, 670, 80, 690);
                opt2.OptionName = "Item2";
                opt2.Border = new Border(opt2);
                opt2.Border.Width = 1;
                opt2.Characteristics.Border = System.Drawing.Color.Black;

                RadioButtonOptionField opt3 = new RadioButtonOptionField();
                opt3.Rect = new Aspose.Pdf.Rectangle(80, 690, 100, 710);
                opt3.OptionName = "Item3";
                opt3.Border = new Border(opt3);
                opt3.Border.Width = 1;
                opt3.Characteristics.Border = System.Drawing.Color.Black;
                field.Add(opt1);
                field.Add(opt2);
                field.Add(opt3);
                doc.Form.Add(field);

                dataDir = dataDir + "CreateDoc_out.pdf";
                // Save the PDF document
                doc.Save(dataDir);
                // ExEnd:CreateDoc
                Console.WriteLine("\nNew doc with 3 items radio button created successfully.\nFile saved at " + dataDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 3
0
        public static void Run()
        {
            try
            {
                // ExStart:GroupedCheckBoxes
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

                // Instantiate Document object
                Document pdfDocument = new Document();
                // Add a page to PDF file
                Page page = pdfDocument.Pages.Add();
                // Instatiate RadioButtonField object with page number as argument
                RadioButtonField radio = new RadioButtonField(pdfDocument.Pages[1]);
                // Add first radio button option and also specify its origin using Rectangle object
                RadioButtonOptionField opt1 = new RadioButtonOptionField(page, new Aspose.Pdf.Rectangle(0, 0, 20, 20));
                RadioButtonOptionField opt2 = new RadioButtonOptionField(page, new Aspose.Pdf.Rectangle(100, 0, 120, 20));
                opt1.OptionName = "Test1";
                opt2.OptionName = "Test2";

                radio.Add(opt1);
                radio.Add(opt2);
                opt1.Style                  = BoxStyle.Square;
                opt2.Style                  = BoxStyle.Square;
                opt1.Style                  = BoxStyle.Cross;
                opt2.Style                  = BoxStyle.Cross;
                opt1.Border                 = new Border(opt1);
                opt1.Border.Style           = BorderStyle.Solid;
                opt1.Border.Width           = 1;
                opt1.Characteristics.Border = System.Drawing.Color.Black;
                opt2.Border                 = new Border(opt2);
                opt2.Border.Width           = 1;

                opt2.Border.Style           = BorderStyle.Solid;
                opt2.Characteristics.Border = System.Drawing.Color.Black;

                // Add radio button to form object of Document object
                pdfDocument.Form.Add(radio);

                dataDir = dataDir + "GroupedCheckBoxes_out_.pdf";
                // Save the PDF document
                pdfDocument.Save(dataDir);
                // ExEnd:GroupedCheckBoxes
                Console.WriteLine("\nGrouped checkboxes added successfully.\nFile saved at " + dataDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static void Run()
        {
            try
            {
                // ExStart:GroupedCheckBoxes
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

                // Instantiate Document object
                Document pdfDocument = new Document();
                // Add a page to PDF file
                Page page = pdfDocument.Pages.Add();
                // Instatiate RadioButtonField object with page number as argument
                RadioButtonField radio = new RadioButtonField(pdfDocument.Pages[1]);
                // Add first radio button option and also specify its origin using Rectangle object
                RadioButtonOptionField opt1 = new RadioButtonOptionField(page, new Aspose.Pdf.Rectangle(0, 0, 20, 20));
                RadioButtonOptionField opt2 = new RadioButtonOptionField(page, new Aspose.Pdf.Rectangle(100, 0, 120, 20));
                opt1.OptionName = "Test1";
                opt2.OptionName = "Test2";

                radio.Add(opt1);
                radio.Add(opt2);
                opt1.Style = BoxStyle.Square;
                opt2.Style = BoxStyle.Square;
                opt1.Style = BoxStyle.Cross;
                opt2.Style = BoxStyle.Cross;
                opt1.Border = new Border(opt1);
                opt1.Border.Style = BorderStyle.Solid;
                opt1.Border.Width = 1;
                opt1.Characteristics.Border = System.Drawing.Color.Black;
                opt2.Border = new Border(opt2);
                opt2.Border.Width = 1;

                opt2.Border.Style = BorderStyle.Solid;
                opt2.Characteristics.Border = System.Drawing.Color.Black;
                
                // Add radio button to form object of Document object
                pdfDocument.Form.Add(radio);

                dataDir = dataDir + "GroupedCheckBoxes_out.pdf";
                // Save the PDF document
                pdfDocument.Save(dataDir);
                // ExEnd:GroupedCheckBoxes
                Console.WriteLine("\nGrouped checkboxes added successfully.\nFile saved at " + dataDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 5
0
        private static string CheckFields(Document doc, int pageCount, string filename, string fields, double ratio, bool licensed = true)
        {
            double marginLeft = doc.Pages[pageCount].PageInfo.Margin.Left;
            double marginTop  = doc.Pages[pageCount].PageInfo.Margin.Top;

            int fieldcounter = 0;

            Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(doc);

            // Get values from all fields
            foreach (Field formField in doc.Form.Fields)
            {
                double lowerLeftY = (doc.Pages[pageCount].Rect.Height) - (formField.Rect.ToRect().Y + formField.Height);

                double lowerLeftX = formField.Rect.ToRect().X;
                var    fieldType  = formField.GetType().Name;             //pdfForm.GetFieldType(formField.FullName);
                var    imValue    = "";

                if (fieldType.ToString() == "CheckboxField" || fieldType.ToString() == "ComboBoxField" || fieldType.ToString() == "RadioButtonOptionField" || fieldType.ToString() == "TextBoxField")
                {
                    var value = formField.Value;

                    if (fieldType.ToString() == "TextBoxField")
                    {
                        fieldType = "Text";
                    }
                    if (fieldType.ToString() == "CheckboxField")
                    {
                        CheckboxField field = (CheckboxField)formField;
                        if (field.Parent != null)
                        {
                            imValue = field.Parent.FullName;
                        }
                        fieldType = "CheckBox";
                        if (field.Checked)
                        {
                            value = "true";
                        }
                        else
                        {
                            value = "false";
                        }
                    }
                    if (fieldType.ToString() == "RadioButtonOptionField")
                    {
                        RadioButtonOptionField field = (RadioButtonOptionField)formField;
                        RadioButtonField       rbf   = (RadioButtonField)field.Parent;

                        fieldType = "Radio";
                        if (field.Parent != null)
                        {
                            imValue = field.Parent.FullName;
                        }
                        if ((rbf.Options[field.OptionName].Index == rbf.Selected))
                        {
                            value = "true";
                        }
                        else
                        {
                            value = "false";
                        }
                    }
                    if (fieldType.ToString() == "ComboBoxField")
                    {
                        ComboBoxField field     = (ComboBoxField)formField;
                        string        optValues = value;
                        fieldType = "ComboBox";
                        foreach (Option opt in field.Options)
                        {
                            optValues = optValues + "^^^" + opt.Value;
                        }
                        value = optValues;
                    }

                    bool isRequired = pdfForm.IsRequiredField(formField.FullName);
                    //fields += "$#$" + (lowerLeftX * 2.08) * ratio + "$#$" + (lowerLeftY * 2.08) * ratio + "$#$" + (formField.Width * 2.08) * ratio + "$#$" + (formField.Height * 2.08) * ratio + "$#$" + formField.PageIndex + "$#$" + "image" + formField.PageIndex + ".png" + "$#$" + value + "$#$" + formField.DefaultAppearance.FontName + "$#$" + formField.DefaultAppearance.FontSize + "$#$" + " " + "$#$" + " " + "$#$" + " " + "$#$" + ratio + "$#$" + " " + "$#$" + formField.FullName.Replace('/', '-') + "$#$" + fieldType;
                    fields += "$#$" + (lowerLeftX * 2.08) * ratio + "$#$" + (lowerLeftY * 2.08) * ratio + "$#$" + (formField.Width * 2.08) * ratio + "$#$" + (formField.Height * 2.08) * ratio + "$#$" + formField.PageIndex + "$#$" + "image" + formField.PageIndex + ".png" + "$#$" + value + "$#$" + formField.DefaultAppearance.FontName + "$#$" + formField.DefaultAppearance.FontSize + "$#$" + " " + "$#$" + " " + "$#$" + isRequired + "$#$" + ratio + "$#$" + imValue + "$#$" + fieldcounter + "$#$" + fieldType;
                }
                int length = fields.Length;
                fieldcounter += 1;
                if (!licensed && fieldcounter == 5)
                {
                    break;
                }
            }



            return(fields);
        }
Exemplo n.º 6
0
        public IHttpActionResult UploadPic(UploadPicModel uploadPicModel)
        {
            var      fullPath         = Path.Combine(Config.Configuration.WorkingDirectory, "Editor", uploadPicModel.documentId);
            var      downloadFileName = "document.pdf";
            var      documentFileName = Path.Combine(Config.Configuration.WorkingDirectory, "Editor", uploadPicModel.documentId, downloadFileName);
            Document doc = new Document(documentFileName);

            //Create image stamp
            ImageStamp imageStamp = new ImageStamp(HttpContext.Current.Server.MapPath("/img/Test.png"));

            for (int i = 0; i < uploadPicModel.shapes.Count; i++)
            {
                //create stamp
                Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();

                float shapeX = (uploadPicModel.shapes[i].x * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio);
                float shapeY = (uploadPicModel.shapes[i].y * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio);
                float shapeW = (uploadPicModel.shapes[i].w * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio);
                float shapeH = (uploadPicModel.shapes[i].h * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio);

                double yaxis = (float)(doc.Pages[uploadPicModel.shapes[i].p].Rect.URY - (shapeH + shapeY));

                var isSpecial = true;                //regexItem.IsMatch(uploadPicModel.shapes[i].imName);

                if (uploadPicModel.shapes[i].Itype == "highlight" || uploadPicModel.shapes[i].Itype == "image")
                {
                    imageStamp = uploadPicModel.shapes[i].Itype == "highlight"
                                                ? new ImageStamp(HttpContext.Current.Server.MapPath("/img/test.png"))
                                                : new ImageStamp(fullPath + uploadPicModel.shapes[i].imName);

                    imageStamp.Background = false;
                    imageStamp.XIndent    = (float)(shapeX);
                    imageStamp.YIndent    = (float)(yaxis);
                    imageStamp.Height     = shapeH;
                    imageStamp.Width      = shapeW;

                    //Add stamp to particular page
                    doc.Pages[uploadPicModel.shapes[i].p].AddStamp(imageStamp);
                }
                else if (uploadPicModel.shapes[i].Itype == "text")
                {
                    //create text stamp
                    TextStamp textStamp = new TextStamp(uploadPicModel.shapes[i].t);
                    textStamp.XIndent = (float)(shapeX);
                    textStamp.YIndent = (float)(yaxis);
                    //rotate stamp
                    textStamp.RotateAngle = 360 - (Convert.ToDouble(uploadPicModel.shapes[i].fieldType) * 180 / Math.PI);

                    //set text properties
                    textStamp.TextState.Font     = FontRepository.FindFont(uploadPicModel.shapes[i].n);
                    textStamp.TextState.FontSize = Convert.ToInt32(uploadPicModel.shapes[i].s) * 0.75f;

                    if (uploadPicModel.shapes[i].wt == "bold")
                    {
                        textStamp.TextState.FontStyle = FontStyles.Bold;
                    }

                    if (uploadPicModel.shapes[i].st == "italic")
                    {
                        textStamp.TextState.FontStyle = FontStyles.Italic;
                    }


                    textStamp.TextState.ForegroundColor = GetColor(uploadPicModel.shapes[i].c);
                    //add stamp to particular page
                    doc.Pages[uploadPicModel.shapes[i].p].AddStamp(textStamp);
                }
                else if (uploadPicModel.shapes[i].Itype == "field" && isSpecial)
                {
                    if (uploadPicModel.shapes[i].fieldType == "Text")
                    {
                        // Get a field
                        TextBoxField textBoxField = doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)] as TextBoxField;
                        // Modify field value
                        textBoxField.Value = uploadPicModel.shapes[i].t;
                    }
                    else if (uploadPicModel.shapes[i].fieldType == "CheckBox")
                    {
                        // Get a field
                        CheckboxField checkBoxField = doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)] as CheckboxField;
                        if (uploadPicModel.shapes[i].t != "")
                        {
                            // Modify field value
                            checkBoxField.Checked = Convert.ToBoolean(uploadPicModel.shapes[i].t);
                        }
                    }
                    else if (uploadPicModel.shapes[i].fieldType == "Radio")
                    {
                        RadioButtonOptionField field = (RadioButtonOptionField)doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)];

                        RadioButtonField rbf = (RadioButtonField)field.Parent;
                        if (Convert.ToBoolean(uploadPicModel.shapes[i].t))
                        {
                            rbf.Selected = rbf.Options[field.OptionName].Index;
                        }
                        else
                        {
                            field.ActiveState = "Off";
                        }
                    }
                    else if (uploadPicModel.shapes[i].fieldType == "ComboBox")
                    {
                        // Get a field
                        ComboBoxField comboBoxField = doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)] as ComboBoxField;
                        var           values        = uploadPicModel.shapes[i].t.Split(new[] { "^^^" }, StringSplitOptions.None)[0];

                        foreach (var item in comboBoxField.Options.Cast <Option>())
                        {
                            if (item.Value == values)
                            {
                                comboBoxField.Selected = item.Index;
                            }
                        }
                    }
                }
            }

            doc.Save(Config.Configuration.OutputDirectory + "/Export.pdf");

            return(Ok(new DocStatusModel()));
        }
Exemplo n.º 7
0
        public static void UploadPic(List <shap> shapes, string filename, string aspectRatio)
        {
            Document doc = new Document(HttpContext.Current.Server.MapPath("Convert/output.pdf"));

            //Create image stamp
            ImageStamp imageStamp = new ImageStamp(HttpContext.Current.Server.MapPath("test.png"));

            for (int i = 0; i < shapes.Count; i++)
            {
                //create stamp
                Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();

                float shapeX = (shapes[i].x * 72 / 150) / (float)Convert.ToDouble(shapes[i].ratio);
                float shapeY = (shapes[i].y * 72 / 150) / (float)Convert.ToDouble(shapes[i].ratio);
                float shapeW = (shapes[i].w * 72 / 150) / (float)Convert.ToDouble(shapes[i].ratio);
                float shapeH = (shapes[i].h * 72 / 150) / (float)Convert.ToDouble(shapes[i].ratio);

                double yaxis = (float)(doc.Pages[shapes[i].p].Rect.URY - (shapeH + shapeY));

                var isSpecial = true;//regexItem.IsMatch(shapes[i].imName);

                if (shapes[i].Itype == "highlight" || shapes[i].Itype == "image")
                {
                    if (shapes[i].Itype == "highlight")
                    {
                        imageStamp = new ImageStamp(HttpContext.Current.Server.MapPath("test.png"));
                    }
                    else
                    {
                        imageStamp = new ImageStamp(HttpContext.Current.Server.MapPath("Images/" + shapes[i].imName));
                    }

                    imageStamp.Background = false;
                    imageStamp.XIndent    = (float)(shapeX);
                    imageStamp.YIndent    = (float)(yaxis);
                    imageStamp.Height     = shapeH;
                    imageStamp.Width      = shapeW;

                    //Add stamp to particular page
                    doc.Pages[shapes[i].p].AddStamp(imageStamp);
                }
                else if (shapes[i].Itype == "text")
                {
                    /*
                     * // create TextBuilder for first page
                     * TextBuilder tb = new TextBuilder(doc.Pages[shapes[i].p]);
                     *
                     * // TextFragment with sample text
                     * TextFragment fragment = new TextFragment(shapes[i].t);
                     *
                     * // set the font for TextFragment
                     * fragment.TextState.Font = FontRepository.FindFont(shapes[i].n);
                     * fragment.TextState.FontSize = Convert.ToInt32(shapes[i].s);
                     * if (shapes[i].wt == "bold")
                     * {
                     *  fragment.TextState.FontStyle = FontStyles.Bold;
                     * }
                     *
                     * if (shapes[i].st == "italic")
                     * {
                     *  fragment.TextState.FontStyle = FontStyles.Italic;
                     * }
                     *
                     * // set the formatting of text as Underline
                     * // fragment.TextState.Underline = true;
                     * fragment.TextState.ForegroundColor = GetColor(shapes[i].c);
                     * // specify the position where TextFragment needs to be placed
                     * fragment.Position = new Position((float)(shapeX), (float)(yaxis));
                     *
                     * // fragment.Rectangle.Rotate((360 - (Convert.ToDouble(shapes[i].fieldType)) * 180 / Math.PI);
                     *
                     * // append TextFragment to PDF file
                     * tb.AppendText(fragment);
                     */

                    //create text stamp
                    TextStamp textStamp = new TextStamp(shapes[i].t);
                    //set whether stamp is background
                    // textStamp.Background = true;
                    //set origin
                    textStamp.XIndent = (float)(shapeX);
                    textStamp.YIndent = (float)(yaxis);
                    //rotate stamp
                    textStamp.RotateAngle = 360 - ((Convert.ToDouble(shapes[i].fieldType)) * 180 / Math.PI);

                    //set text properties
                    textStamp.TextState.Font     = FontRepository.FindFont(shapes[i].n);
                    textStamp.TextState.FontSize = Convert.ToInt32(shapes[i].s) * 0.75f;

                    if (shapes[i].wt == "bold")
                    {
                        textStamp.TextState.FontStyle = FontStyles.Bold;
                    }

                    if (shapes[i].st == "italic")
                    {
                        textStamp.TextState.FontStyle = FontStyles.Italic;
                    }


                    textStamp.TextState.ForegroundColor = GetColor(shapes[i].c);
                    //add stamp to particular page
                    doc.Pages[shapes[i].p].AddStamp(textStamp);
                }
                else if (shapes[i].Itype == "field" && isSpecial)
                {
                    if (shapes[i].fieldType == "Text")
                    {
                        // Get a field
                        TextBoxField textBoxField = doc.Form.Fields[Convert.ToInt32(shapes[i].imName)] as TextBoxField;
                        // Modify field value
                        textBoxField.Value = shapes[i].t;
                    }
                    else if (shapes[i].fieldType == "CheckBox")
                    {
                        // Get a field
                        CheckboxField checkBoxField = doc.Form.Fields[Convert.ToInt32(shapes[i].imName)] as CheckboxField;
                        if (shapes[i].t != "")
                        {
                            // Modify field value
                            checkBoxField.Checked = Convert.ToBoolean(shapes[i].t);
                        }
                    }
                    else if (shapes[i].fieldType == "Radio")
                    {
                        RadioButtonOptionField field = (RadioButtonOptionField)doc.Form.Fields[Convert.ToInt32(shapes[i].imName)];

                        RadioButtonField rbf = (RadioButtonField)field.Parent;
                        if (Convert.ToBoolean(shapes[i].t))
                        {
                            rbf.Selected = rbf.Options[field.OptionName].Index;
                        }
                        else
                        {
                            field.ActiveState = "Off";
                        }
                    }
                    else if (shapes[i].fieldType == "ComboBox")
                    {
                        // Get a field
                        ComboBoxField comboBoxField = doc.Form.Fields[Convert.ToInt32(shapes[i].imName)] as ComboBoxField;
                        var           values        = shapes[i].t.Split(new string[] { "^^^" }, StringSplitOptions.None)[0];

                        foreach (var item in comboBoxField.Options.Cast <Option>())
                        {
                            if (item.Value == values)
                            {
                                comboBoxField.Selected = item.Index;
                            }
                        }
                    }
                }
            }

            doc.Save(HttpContext.Current.Server.MapPath("Convert/Export.pdf"));
        }
        public static void Run()
        {
            try
            {
                // ExStart:RadioButtonWithOptions
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

                Document         doc   = new Document();
                Page             page  = doc.Pages.Add();
                Aspose.Pdf.Table table = new Aspose.Pdf.Table();
                table.ColumnWidths = "120 120 120";
                page.Paragraphs.Add(table);
                Row  r1 = table.Rows.Add();
                Cell c1 = r1.Cells.Add();
                Cell c2 = r1.Cells.Add();
                Cell c3 = r1.Cells.Add();

                RadioButtonField rf = new RadioButtonField(page);
                rf.PartialName = "radio";
                doc.Form.Add(rf, 1);

                RadioButtonOptionField opt1 = new RadioButtonOptionField();
                RadioButtonOptionField opt2 = new RadioButtonOptionField();
                RadioButtonOptionField opt3 = new RadioButtonOptionField();

                opt1.OptionName = "Item1";
                opt2.OptionName = "Item2";
                opt3.OptionName = "Item3";

                opt1.Width  = 15;
                opt1.Height = 15;
                opt2.Width  = 15;
                opt2.Height = 15;
                opt3.Width  = 15;
                opt3.Height = 15;

                rf.Add(opt1);
                rf.Add(opt2);
                rf.Add(opt3);

                opt1.Border                      = new Border(opt1);
                opt1.Border.Width                = 1;
                opt1.Border.Style                = BorderStyle.Solid;
                opt1.Characteristics.Border      = System.Drawing.Color.Black;
                opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
                opt1.Caption                     = new TextFragment("Item1");
                opt2.Border                      = new Border(opt1);
                opt2.Border.Width                = 1;
                opt2.Border.Style                = BorderStyle.Solid;
                opt2.Characteristics.Border      = System.Drawing.Color.Black;
                opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
                opt2.Caption                     = new TextFragment("Item2");
                opt3.Border                      = new Border(opt1);
                opt3.Border.Width                = 1;
                opt3.Border.Style                = BorderStyle.Solid;
                opt3.Characteristics.Border      = System.Drawing.Color.Black;
                opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
                opt3.Caption                     = new TextFragment("Item3");
                c1.Paragraphs.Add(opt1);
                c2.Paragraphs.Add(opt2);
                c3.Paragraphs.Add(opt3);

                dataDir = dataDir + "RadioButtonWithOptions_out.pdf";
                // Save the PDF file
                doc.Save(dataDir);
                // ExEnd:RadioButtonWithOptions
                Console.WriteLine("\nRadio button field with three options added successfully.\nFile saved at " + dataDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 9
0
        private Aspose.Pdf.Forms.Field getFormField(HNodeTag inputNode)
        {
            Aspose.Pdf.Forms.Field field = null;

            string fieldType = inputNode.GetAttribute("type", "text");

            //button|checkbox|file|hidden|image|password|radio|reset|submit|text

            switch (fieldType)
            {
            case "button":
            case "submit":
                field = getFormField_Button();
                break;

            case "checkbox":
                field = getFormField_CheckBox();
                break;

            case "file":
            case "hidden":
            case "image":
            case "password":
            case "reset":
                break;

            case "radio":
                field = getFormField_RadioBox();
                break;

            case "text":
            default:
                field = getFormField_TextBox();
                break;
            }

            return(field);

            Aspose.Pdf.Forms.Field getFormField_TextBox()
            {
                TextBoxField textBoxField = new TextBoxField();

                textBoxField.Height = 16;
                textBoxField.Width  = 160;

                string name = inputNode.GetAttribute("name", "text_box_field");

                textBoxField.PartialName = name;

                string value = inputNode.GetAttribute("value", "");

                if (!String.IsNullOrEmpty(value))
                {
                    textBoxField.Value = value;
                }
                //Так делать нельзя, падает исключение при pdfDocument.ProcessParagraphs();
                //textBoxField.Value = "";

                //Border border = new Border(field);
                //border.Width = 1;
                //border.Dash = new Dash(1, 1);
                //field.Border = border;

                return(textBoxField);
            }

            Aspose.Pdf.Forms.Field getFormField_CheckBox()
            {
                CheckboxField checkBoxField = new CheckboxField();

                checkBoxField.Height = 10;
                checkBoxField.Width  = 10;

                string name = inputNode.GetAttribute("name", "checkbox_field");

                checkBoxField.PartialName = name;

                //Так делать нельзя, падает исключение при pdfDocument.ProcessParagraphs();
                //checkBoxField.Checked = true;

                return(checkBoxField);
            }

            Aspose.Pdf.Forms.Field getFormField_RadioBox()
            {
                string           name = inputNode.GetAttribute("name", "radio_button_field");
                RadioButtonField rbf  = getRadioButtonField(name);

                RadioButtonOptionField opt = new RadioButtonOptionField();

                opt.OptionName = name + "_" + (rbf.Count + 1).ToString();
                opt.Width      = 12;
                opt.Height     = 12;

                //Так делать нельзя, падает исключение при pdfDocument.ProcessParagraphs();
                //opt.Border = new Border(opt);
                //opt.Border.Width = 1;
                //opt.Border.Style = BorderStyle.Solid;
                //opt.Characteristics.Border = System.Drawing.Color.Black;
                //opt.DefaultAppearance.TextColor = System.Drawing.Color.Red;

                //opt.Caption = new TextFragment("Item1");


                rbf.Add(opt);

                return(opt);
            }

            Aspose.Pdf.Forms.Field getFormField_Button()
            {
                ButtonField buttonField = new ButtonField();

                string value = inputNode.GetAttribute("value", "");

                buttonField.Height = 18;
                buttonField.Width  = 12 + 6 * value.Length;

                if (!String.IsNullOrEmpty(value))
                {
                    buttonField.Value = value;
                }

                return(buttonField);
            }
        }
        public static void Run()
        {
            try
            {
                // ExStart:RadioButtonWithOptions
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

                Document doc = new Document();
                Page page = doc.Pages.Add();
                Aspose.Pdf.Table table = new Aspose.Pdf.Table();
                table.ColumnWidths = "120 120 120";
                page.Paragraphs.Add(table);
                Row r1 = table.Rows.Add();
                Cell c1 = r1.Cells.Add();
                Cell c2 = r1.Cells.Add();
                Cell c3 = r1.Cells.Add();

                RadioButtonField rf = new RadioButtonField(page);
                rf.PartialName = "radio";
                doc.Form.Add(rf, 1);

                RadioButtonOptionField opt1 = new RadioButtonOptionField();
                RadioButtonOptionField opt2 = new RadioButtonOptionField();
                RadioButtonOptionField opt3 = new RadioButtonOptionField();

                opt1.OptionName = "Item1";
                opt2.OptionName = "Item2";
                opt3.OptionName = "Item3";

                opt1.Width = 15;
                opt1.Height = 15;
                opt2.Width = 15;
                opt2.Height = 15;
                opt3.Width = 15;
                opt3.Height = 15;

                rf.Add(opt1);
                rf.Add(opt2);
                rf.Add(opt3);

                opt1.Border = new Border(opt1);
                opt1.Border.Width = 1;
                opt1.Border.Style = BorderStyle.Solid;
                opt1.Characteristics.Border = System.Drawing.Color.Black;
                opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
                opt1.Caption = new TextFragment("Item1");
                opt2.Border = new Border(opt1);
                opt2.Border.Width = 1;
                opt2.Border.Style = BorderStyle.Solid;
                opt2.Characteristics.Border = System.Drawing.Color.Black;
                opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
                opt2.Caption = new TextFragment("Item2");
                opt3.Border = new Border(opt1);
                opt3.Border.Width = 1;
                opt3.Border.Style = BorderStyle.Solid;
                opt3.Characteristics.Border = System.Drawing.Color.Black;
                opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
                opt3.Caption = new TextFragment("Item3");
                c1.Paragraphs.Add(opt1);
                c2.Paragraphs.Add(opt2);
                c3.Paragraphs.Add(opt3);

                dataDir = dataDir + "RadioButtonWithOptions_out.pdf";
                // Save the PDF file
                doc.Save(dataDir);
                // ExEnd:RadioButtonWithOptions
                Console.WriteLine("\nRadio button field with three options added successfully.\nFile saved at " + dataDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }