예제 #1
0
        private Aspose.Pdf.Forms.Field getFormFieldButton(HNodeTag buttonNode)
        {
            Aspose.Pdf.Forms.Field field = null;
            string value = "";

            if ((buttonNode is HNodeContainer) && (buttonNode as HNodeContainer).ChildNodes.Count > 0)
            {
                if ((buttonNode as HNodeContainer).ChildNodes[0] is HNodeText)
                {
                    value = ((buttonNode as HNodeContainer).ChildNodes[0] as HNodeText).Text;
                }
            }

            field = getFormField_Button();
            return(field);

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

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

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

                return(buttonField);
            }
        }
예제 #2
0
        public PDocument(string fileFullName, HDocument hDocument)
        {
            /*
             * //DEBUG
             * PDocument_debug(fileFullName, hDocument);
             * return;
             */


            bodyNode = hDocument.BodyNode;

            pageTextState = PUtil.TextStateUtil.TextState_Default();
            PUtil.TextStateUtil.TextState_ModifyFromHStyles((bodyNode as HNodeTag).Styles, pageTextState);

            pageMargin     = new MarginInfo(4, 4, 4, 12);
            pageBackground = Aspose.Pdf.Color.FromRgb(1.00, 1.00, 1.00);

            pdfDocument           = new Document();
            pdfPage               = null;
            pdfTextFragment       = null;
            pdfImage              = null;
            hyperlinkNode         = null;
            pdfNewLine            = null;
            inlineParagraphMargin = null;
            pdfFormField          = null;
            pdfRadioButtonFields  = new Dictionary <string, RadioButtonField>();

            updateCurrentPage();
            createBody();

            pdfDocument.Save(fileFullName);
        }
        public static void Run()
        {
            // ExStart:FormFieldFont14
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

            // Open document
            Document pdfDocument = new Document(dataDir + "FormFieldFont14.pdf");

            // Get particular form field from document
            Aspose.Pdf.Forms.Field field = pdfDocument.Form["textbox1"] as Aspose.Pdf.Forms.Field;

            // Create font object
            Aspose.Pdf.Text.Font font = FontRepository.FindFont("ComicSansMS");

            // Set the font information for form field
            // Field.DefaultAppearance = new Aspose.Pdf.Forms.in.DefaultAppearance(font, 10, System.Drawing.Color.Black);

            dataDir = dataDir + "FormFieldFont14_out.pdf";
            // Save updated document
            pdfDocument.Save(dataDir);
            // ExEnd:FormFieldFont14
            Console.WriteLine("\nForm field font setup successfully.\nFile saved at " + dataDir);
        }
예제 #4
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);
            }
        }
예제 #5
0
        private TextSegment getTextSegment(HNode node, TextState parentTextState)
        {
            TextSegment textSegment = null;

            // Text element
            if (node is HNodeText)
            {
                textSegment           = new TextSegment();
                textSegment.TextState = parentTextState;
                textSegment.Text      = (node as HNodeText).Text;


                if (hyperlinkNode != null)
                {
                    Aspose.Pdf.WebHyperlink pdfHyperlink = new WebHyperlink(hyperlinkNode.GetAttribute("href", "#"));
                    textSegment.Hyperlink = pdfHyperlink;
                }
            }
            // New Line element <br />
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.br)
            {
                /*
                 * //слетают стили!!!
                 * textSegment = new TextSegment();
                 * //textSegment.TextState = parentTextState;
                 * textSegment.Text = Environment.NewLine;
                 */

                pdfNewLine      = new TextSegment();
                pdfNewLine.Text = Environment.NewLine;
            }
            // Hyperlink element <a>
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.a)
            {
                PUtil.TextStateUtil.TextState_ModifyForHyperlink(parentTextState);
                PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, parentTextState);
            }
            // Bold text element <b>
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.b)
            {
                PUtil.TextStateUtil.TextState_ModifyForBold(parentTextState);
                PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, parentTextState);
            }
            // Italic text element <i>
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.i)
            {
                PUtil.TextStateUtil.TextState_ModifyForItalic(parentTextState);
                PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, parentTextState);
            }
            // Image element <img>
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.img)
            {
                pdfImage = getImage(node as HNodeTag);
            }
            // Form field element <input>
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.input)
            {
                pdfFormField = getFormField(node as HNodeTag);
            }
            // Button element <button>
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.button)
            {
                pdfFormField = getFormFieldButton(node as HNodeTag);
            }

            // TODO - other inline tags


            return(textSegment);
        }
예제 #6
0
        private void createNode(HNode node, TextState parentTextState)
        {
            TextState nodeTextState = new TextState();

            nodeTextState.ApplyChangesFrom(parentTextState);

            if (node is HNodeTag)
            {
                PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, nodeTextState);
            }

            // Block element
            if ((node is HNodeTag) && HUtil.TagUtil.IsBlockTag((node as HNodeTag).TagType))
            {
                addTextFragmentOnPage();
                createTextFragmentByTagType((node as HNodeTag).TagType);
            }
            // Inline element or Text element
            else if (
                (node is HNodeTag) && HUtil.TagUtil.IsInlineTag((node as HNodeTag).TagType)
                ||
                (node is HNodeText)
                )
            {
                if ((node is HNodeText) && (node as HNodeText).ParentNode != null && ((node as HNodeText).ParentNode is HNodeTag) && (((node as HNodeText).ParentNode as HNodeTag)).TagType == HTagType.button)
                {
                    //
                }
                else
                {
                    // Create TextSegment for element
                    TextSegment textSegment = getTextSegment(node, nodeTextState);

                    // New Line, <BR />
                    if (pdfNewLine != null)
                    {
                        double marginTop    = 0;
                        double marginBottom = 0;
                        if (pdfTextFragment != null)
                        {
                            marginBottom = pdfTextFragment.Margin.Bottom;
                            pdfTextFragment.Margin.Bottom = 0;
                        }

                        addTextFragmentOnPage();
                        createTextFragmentByTagType(HTagType.div);

                        if (pdfTextFragment != null)
                        {
                            pdfTextFragment.Margin.Top    = marginTop;
                            pdfTextFragment.Margin.Bottom = marginBottom;
                        }

                        pdfNewLine = null;
                    }
                    // Image
                    else if (pdfImage != null)
                    {
                        double     imageHeight = pdfImage.FixHeight;
                        MarginInfo margin      = new MarginInfo(0, 12, 0, 12);
                        if (pdfTextFragment == null || pdfTextFragment.Segments.Count == 0 || (pdfTextFragment.Segments.Count == 1 && pdfTextFragment.Segments[1].Text == String.Empty))
                        {
                        }
                        else
                        {
                            pdfTextFragment.Margin.Top += imageHeight;
                            margin = new MarginInfo(0, pdfTextFragment.Margin.Bottom, 0, -1 * imageHeight);
                        }

                        addTextFragmentOnPage(false);

                        pdfImage.IsInLineParagraph = true;
                        pdfImage.Margin            = margin;
                        inlineParagraphMargin      = margin;


                        if (hyperlinkNode != null)
                        {
                            Aspose.Pdf.WebHyperlink pdfHyperlink = new WebHyperlink(hyperlinkNode.GetAttribute("href", "#"));
                            pdfImage.Hyperlink = pdfHyperlink;
                        }

                        pdfPage.Paragraphs.Add(pdfImage);

                        if (node.NextNode == null)
                        {
                            updateCurrentPage();
                        }

                        pdfImage = null;
                    }
                    // Form Field Element
                    else if (pdfFormField != null)
                    {
                        //
                        //
                        //

                        double     inputHeight = pdfFormField.Height;
                        MarginInfo margin      = new MarginInfo(0, 12, 0, 12);
                        if (pdfTextFragment == null || pdfTextFragment.Segments.Count == 0 || (pdfTextFragment.Segments.Count == 1 && pdfTextFragment.Segments[1].Text == String.Empty))
                        {
                        }
                        else
                        {
                            double textFragmentHeight = pdfTextFragment.Rectangle.Height;

                            margin = pdfTextFragment.Margin;

                            pdfTextFragment.Margin.Bottom = textFragmentHeight - inputHeight;
                            pdfTextFragment.Margin.Top   += Math.Max(0, (inputHeight - textFragmentHeight));

                            pdfTextFragment.Margin.Top += inputHeight;
                        }


                        addTextFragmentOnPage(false);

                        pdfFormField.IsInLineParagraph = true;
                        pdfFormField.Margin            = margin;
                        inlineParagraphMargin          = new MarginInfo(pdfFormField.Width, margin.Bottom, margin.Right, margin.Top);


                        pdfPage.Paragraphs.Add(pdfFormField);

                        if (node.NextNode == null)
                        {
                            updateCurrentPage();
                        }

                        pdfFormField = null;
                    }
                    // TextFragment for InLineParagraph mode
                    else if (pdfTextFragment == null)
                    {
                        HTagType tagTypeForTextFragment           = HTagType.div;
                        bool     isInLineParagraphForTextFragment = false;


                        bool flagPreviousImage = false;
                        bool flagPreviousInput = false;

                        if (node.PrevNode != null && (node.PrevNode is HNodeTag) && (node.PrevNode as HNodeTag).TagType == HTagType.img)
                        {
                            // prev image element
                            if (node.ParentNode != null && (node.ParentNode is HNodeTag) && HUtil.TagUtil.IsBlockTag((node.ParentNode as HNodeTag).TagType))
                            {
                                tagTypeForTextFragment = (node.ParentNode as HNodeTag).TagType;
                            }

                            isInLineParagraphForTextFragment = true;
                            flagPreviousImage = true;
                        }
                        else if (node.PrevNode != null && (node.PrevNode is HNodeTag) && (node.PrevNode as HNodeTag).TagType == HTagType.input)
                        {
                            // prev input element
                            if (node.ParentNode != null && (node.ParentNode is HNodeTag) && HUtil.TagUtil.IsBlockTag((node.ParentNode as HNodeTag).TagType))
                            {
                                tagTypeForTextFragment = (node.ParentNode as HNodeTag).TagType;
                            }

                            isInLineParagraphForTextFragment = true;
                            flagPreviousInput = true;
                        }
                        else
                        {
                        }

                        createTextFragmentByTagType(tagTypeForTextFragment);
                        pdfTextFragment.IsInLineParagraph = isInLineParagraphForTextFragment;


                        if ((flagPreviousImage || flagPreviousInput) && inlineParagraphMargin != null)
                        {
                            pdfTextFragment.Margin.Top    = -1 * pdfTextFragment.Rectangle.Height - inlineParagraphMargin.Bottom;
                            pdfTextFragment.Margin.Bottom = inlineParagraphMargin.Bottom;

                            pdfTextFragment.Margin.Left = inlineParagraphMargin.Left;

                            inlineParagraphMargin = null;
                        }
                    }



                    if (textSegment != null && pdfTextFragment != null)
                    //if (textSegment != null)
                    {
                        pdfTextFragment.Segments.Add(textSegment);
                    }
                }
            }

            //
            // Create Nodes recursively with consider the hyperlink
            //
            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.a)
            {
                hyperlinkNode = (node as HNodeTag);
            }

            if (node is HNodeContainer)
            {
                foreach (HNode childNode in (node as HNodeContainer).ChildNodes)
                {
                    createNode(childNode, nodeTextState);
                }
            }

            if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.a)
            {
                hyperlinkNode = null;
            }
            //
            //
            //


            //
            // Add Text Fragment on Page (if need)
            //
            if ((node is HNodeTag) && HUtil.TagUtil.IsBlockTag((node as HNodeTag).TagType))
            {
                addTextFragmentOnPage();
            }
            //
            //
            //
        }