예제 #1
0
        private void createTextFragmentByTagType(HTagType tagType)
        {
            if (!HUtil.TagUtil.IsBlockTag(tagType))
            {
                pdfTextFragment = null;
                return;
            }

            pdfTextFragment = new TextFragment();

            if (tagType == HTagType.div)
            {
                pdfTextFragment.Margin = new MarginInfo(0, 0, 0, 0);
            }
            else if (tagType == HTagType.p)
            {
                pdfTextFragment.Margin = new MarginInfo(0, 12, 0, 12);
            }
            else
            {
                pdfTextFragment.Margin = new MarginInfo(0, 0, 0, 0);
            }

            return;
        }
        public void GetTagTypeByTagName_IMG()
        {
            // arrange
            string   tagName  = "IMG";
            HTagType expected = HTagType.img;

            // act
            HTagType actual = HUtil.EnumUtil.GetTagTypeByTagName(tagName);

            // assert
            Assert.AreEqual(expected, actual);
        }
        public void GetTagTypeByTagName_SPAN()
        {
            // arrange
            string   tagName  = "span";
            HTagType expected = HTagType.span;

            // act
            HTagType actual = HUtil.EnumUtil.GetTagTypeByTagName(tagName);

            // assert
            Assert.AreEqual(expected, actual);
        }
예제 #4
0
            public static bool IsBlockTag(HTagType tagType)
            {
                switch (tagType)
                {
                case HTagType.div:
                case HTagType.form:
                case HTagType.p:
                    return(true);

                case HTagType._unknown:
                default:
                    return(false);
                }
            }
예제 #5
0
            public static bool NeedEndTag(HTagType tagType)
            {
                switch (tagType)
                {
                case HTagType.br:
                case HTagType.img:
                case HTagType.input:
                case HTagType._unknown:
                    return(false);

                default:
                    return(true);
                }
            }
예제 #6
0
            public static bool IsInlineTag(HTagType tagType)
            {
                switch (tagType)
                {
                case HTagType.span:
                case HTagType.br:
                case HTagType.b:
                case HTagType.i:
                case HTagType.a:
                case HTagType.img:
                case HTagType.input:
                case HTagType.button:
                    return(true);

                case HTagType._unknown:
                default:
                    return(false);
                }
            }
예제 #7
0
 public HNodeSole(HTagType tagType, IEnumerable <HAttribute> attributes, IEnumerable <HStyle> styles) : base(tagType, attributes, styles)
 {
 }
예제 #8
0
 public static string GetTagNameByTagType(HTagType tagType)
 {
     return GetEnumDescription(tagType);
 }
예제 #9
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();
            }
            //
            //
            //
        }