/**
         * Adds currentContent list to a paragraph element. If addNewLines is true a
         * Paragraph object is returned, else a NoNewLineParagraph object is
         * returned.
         *
         * @param currentContent IList<IElement> of the current elements to be added.
         * @param addNewLines bool to declare which paragraph element should be
         *            returned, true if new line should be added or not.
         * @param applyCSS true if CSS should be applied on the paragraph
         * @param tag the relevant tag
         * @return a List with paragraphs
         */

        public virtual IList <IElement> CurrentContentToParagraph(IList <IElement> currentContent,
                                                                  bool addNewLines, bool applyCSS, Tag tag,
                                                                  IWorkerContext ctx)
        {
            try
            {
                IList <IElement> list = new List <IElement>();
                if (currentContent.Count > 0)
                {
                    if (addNewLines)
                    {
                        Paragraph p = CreateParagraph();
                        p.MultipliedLeading = 1.2f;
                        foreach (IElement e in currentContent)
                        {
                            if (e is LineSeparator)
                            {
                                try {
                                    HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                                    Chunk newLine = (Chunk)GetCssAppliers().Apply(new Chunk(Chunk.NEWLINE), tag, htmlPipelineContext);
                                    p.Add(newLine);
                                } catch (NoCustomContextException exc) {
                                    throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), exc);
                                }
                            }
                            p.Add(e);
                        }
                        if (p.Trim())
                        {
                            if (applyCSS)
                            {
                                p = (Paragraph)GetCssAppliers().Apply(p, tag, GetHtmlPipelineContext(ctx));
                            }
                            list.Add(p);
                        }
                    }
                    else
                    {
                        NoNewLineParagraph p = new NoNewLineParagraph(float.NaN);
                        p.MultipliedLeading = 1.2f;
                        foreach (IElement e in currentContent)
                        {
                            p.Add(e);
                        }
                        p = (NoNewLineParagraph)GetCssAppliers().Apply(p, tag, GetHtmlPipelineContext(ctx));
                        list.Add(p);
                    }
                }
                return(list);
            } catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(
                          LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
            }
        }
Exemple #2
0
        protected void InvertTextAlignForParagraph(NoNewLineParagraph p)
        {
            switch (p.Alignment)
            {
            case Element.ALIGN_UNDEFINED:
            case Element.ALIGN_CENTER:
            case Element.ALIGN_JUSTIFIED:
            case Element.ALIGN_JUSTIFIED_ALL:
                break;

            case Element.ALIGN_RIGHT:
                p.Alignment = Element.ALIGN_LEFT;
                break;

            case Element.ALIGN_LEFT:
            default:
                p.Alignment = Element.ALIGN_RIGHT;
                break;
            }
        }
Exemple #3
0
        /*
         * (non-Javadoc)
         *
         * @see com.itextpdf.tool.xml.ITagProcessor#content(com.itextpdf.tool.xml.Tag, java.util.List,
         * com.itextpdf.text.Document, java.lang.String)
         */
        public override IList <IElement> Content(IWorkerContext ctx, Tag tag, String content)
        {
            List <Chunk>       sanitizedChunks = HTMLUtils.Sanitize(content, false);
            List <IElement>    l = new List <IElement>(1);
            NoNewLineParagraph sanitizedNoNewLineParagraph = new NoNewLineParagraph();

            foreach (Chunk sanitized in sanitizedChunks)
            {
                Chunk c = GetCssAppliers().ChunkCssAplier.Apply(sanitized, tag);
                sanitizedNoNewLineParagraph.Add(c);
            }
            if (sanitizedNoNewLineParagraph.Count > 0)
            {
                try {
                    l.Add(GetCssAppliers().Apply(sanitizedNoNewLineParagraph, tag, GetHtmlPipelineContext(ctx)));
                } catch (NoCustomContextException e) {
                    throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
                }
            }
            return(l);
        }
Exemple #4
0
        public void SetUp()
        {
            cells     = new List <Element>();
            tag       = new Tag("td", new Dictionary <String, String>());
            basicPara = new NoNewLineParagraph();
            basic     = new Chunk("content");

            cell    = new HtmlCell();
            applier = new HtmlCellCssApplier();


            LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
            Tag parent = new Tag("tr");

            parent.Parent = new Tag("table");
            tag.Parent    = parent;
            basicPara.Add(basic);
            cell.AddElement(basicPara);
            cells.Add(cell);
            config = new HtmlPipelineContext(null);
        }
Exemple #5
0
        /* (non-Javadoc)
         * @see com.itextpdf.tool.xml.ITagProcessor#content(com.itextpdf.tool.xml.Tag, java.lang.String)
         */
        public override IList <IElement> Content(IWorkerContext ctx, Tag tag, String content)
        {
            List <Chunk>       sanitizedChunks = HTMLUtils.Sanitize(content, false);
            List <IElement>    l = new List <IElement>(1);
            NoNewLineParagraph sanitizedNoNewLineParagraph = new NoNewLineParagraph();

            try {
                HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                foreach (Chunk sanitized in sanitizedChunks)
                {
                    sanitizedNoNewLineParagraph.Add(GetCssAppliers().Apply(sanitized, tag, htmlPipelineContext));
                }
                if (sanitizedNoNewLineParagraph.Count > 0)
                {
                    l.Add(GetCssAppliers().Apply(sanitizedNoNewLineParagraph, tag, htmlPipelineContext));
                }
            }
            catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(e);
            }
            return(l);
        }
Exemple #6
0
 /**
  * Adds currentContent list to a paragraph element. If addNewLines is true a
  * Paragraph object is returned, else a NoNewLineParagraph object is
  * returned.
  *
  * @param currentContent IList<IElement> of the current elements to be added.
  * @param addNewLines bool to declare which paragraph element should be
  *            returned, true if new line should be added or not.
  * @param applyCSS true if CSS should be applied on the paragraph
  * @param tag the relevant tag
  * @return a List with paragraphs
  */
 public virtual IList <IElement> CurrentContentToParagraph(IList <IElement> currentContent,
                                                           bool addNewLines, bool applyCSS, Tag tag, IWorkerContext ctx)
 {
     try {
         IList <IElement> list = new List <IElement>();
         if (currentContent.Count > 0)
         {
             if (addNewLines)
             {
                 Paragraph p = new Paragraph();
                 foreach (IElement e in currentContent)
                 {
                     p.Add(e);
                 }
                 if (applyCSS)
                 {
                     p = new ParagraphCssApplier(GetHtmlPipelineContext(ctx)).Apply(p, tag);
                 }
                 list.Add(p);
             }
             else
             {
                 NoNewLineParagraph p = new NoNewLineParagraph();
                 foreach (IElement e in currentContent)
                 {
                     p.Add(e);
                 }
                 p = new NoNewLineParagraphCssApplier(GetHtmlPipelineContext(ctx)).Apply(p, tag);
                 list.Add(p);
             }
             // TODO enhance
         }
         return(list);
     } catch (NoCustomContextException e) {
         throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
     }
 }
Exemple #7
0
        /*
         * (non-Javadoc)
         *
         * @see
         * com.itextpdf.tool.xml.ITagProcessor#endElement(com.itextpdf.tool.xml.Tag,
         * java.util.List, com.itextpdf.text.Document)
         */
        public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent)
        {
            try {
                String name;
                tag.Attributes.TryGetValue(HTML.Attribute.NAME, out name);
                IList <IElement> elems = new List <IElement>(0);
                if (currentContent.Count > 0)
                {
                    NoNewLineParagraph p = new NoNewLineParagraph();
                    String             url;
                    tag.Attributes.TryGetValue(HTML.Attribute.HREF, out url);
                    foreach (IElement e in currentContent)
                    {
                        if (e is Chunk)
                        {
                            if (null != url)
                            {
                                if (url.StartsWith("#"))
                                {
                                    if (LOGGER.IsLogging(Level.TRACE))
                                    {
                                        LOGGER.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.A_LOCALGOTO), url));
                                    }
                                    ((Chunk)e).SetLocalGoto(url.Substring(1));
                                }
                                else
                                {
                                    // TODO check url validity?
                                    if (null != GetHtmlPipelineContext(ctx).GetLinkProvider() && !url.StartsWith("http"))
                                    {
                                        String root = GetHtmlPipelineContext(ctx).GetLinkProvider().GetLinkRoot();
                                        if (root.EndsWith("/") && url.StartsWith("/"))
                                        {
                                            root = root.Substring(0, root.Length - 1);
                                        }
                                        url = root + url;
                                    }
                                    if (LOGGER.IsLogging(Level.TRACE))
                                    {
                                        LOGGER.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.A_EXTERNAL), url));
                                    }
                                    ((Chunk)e).SetAnchor(url);
                                }
                            }
                            else if (null != name)
                            {
                                ((Chunk)e).SetLocalDestination(name);
                                if (LOGGER.IsLogging(Level.TRACE))
                                {
                                    LOGGER.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.A_SETLOCALGOTO), name));
                                }
                            }
                        }
                        p.Add(e);
                    }
                    elems.Add(GetCssAppliers().Apply(p, tag, GetHtmlPipelineContext(ctx)));
                }
                else
                // !currentContent > 0 ; An empty "a" tag has been encountered.
                // we're using an anchor space hack here. without the space, reader
                // does
                // not jump to destination
                if (null != name)
                {
                    if (LOGGER.IsLogging(Level.TRACE))
                    {
                        LOGGER.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.SPACEHACK), name));
                    }
                    elems.Add(new WriteP(name));

                    /*
                     * PdfWriter writer = configuration.GetWriter(); ColumnText c =
                     * new ColumnText(writer.GetDirectContent());
                     * c.SetSimpleColumn(new Phrase(dest), 1,
                     * writer.GetVerticalPosition(false), 1,
                     * writer.GetVerticalPosition(false), 5, Element.ALIGN_LEFT);
                     * try { c.Go(); } catch (DocumentException e) { throw new
                     * RuntimeWorkerException(e); }
                     */
                }
                return(elems);
            } catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
            }
        }
 public void Setup()
 {
     jpegImage = Image.GetInstance(IMAGE);
     paragraph = new NoNewLineParagraph();
 }
Exemple #9
0
        /* (non-Javadoc)
         * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag)
         */
        virtual public NoNewLineParagraph Apply(NoNewLineParagraph p, Tag t, IMarginMemory configuration)
        {
            /*if (this.configuration.GetRootTags().Contains(t.Name)) {
             *  m.SetLeading(t);
             * } else {
             *  m.SetVariablesBasedOnChildren(t);
             * }*/
            float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t);
            float lmb      = 0;
            bool  hasLMB   = false;
            IDictionary <String, String> css = t.CSS;

            foreach (KeyValuePair <String, String> entry in css)
            {
                String key   = entry.Key;
                String value = entry.Value;
                if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key))
                {
                    p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key))
                {
                    p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key))
                {
                    float after = utils.ParseValueToPt(value, fontSize);
                    p.SpacingAfter = p.SpacingAfter + after;
                    lmb            = after;
                    hasLMB         = true;
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key))
                {
                    p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key))
                {
                    p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key))
                {
                    p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key))
                {
                    p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key))
                {
                    p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key))
                {
                    if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value))
                    {
                        p.Alignment = Element.ALIGN_RIGHT;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value))
                    {
                        p.Alignment = Element.ALIGN_CENTER;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value))
                    {
                        p.Alignment = Element.ALIGN_LEFT;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value))
                    {
                        p.Alignment = Element.ALIGN_JUSTIFIED;
                    }
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key))
                {
                    p.FirstLineIndent = utils.ParseValueToPt(value, fontSize);
                }
            }
            // setDefaultMargin to largestFont if no margin-top is set and p-tag is child of the root tag.
            if (null != t.Parent)
            {
                String parent = t.Parent.Name;
                if (!css.ContainsKey(CSS.Property.MARGIN_TOP) && configuration.GetRootTags().Contains(parent))
                {
                    p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(fontSize.ToString(CultureInfo.InvariantCulture) + "pt", 0, configuration);
                }
                if (!css.ContainsKey(CSS.Property.MARGIN_BOTTOM) && configuration.GetRootTags().Contains(parent))
                {
                    p.SpacingAfter = p.SpacingAfter + fontSize;
                    css[CSS.Property.MARGIN_BOTTOM] = fontSize.ToString(CultureInfo.InvariantCulture) + "pt";
                    lmb    = fontSize;
                    hasLMB = true;
                }
                //p.Leading = m.GetLargestLeading();
                if (p.Alignment == -1)
                {
                    p.Alignment = Element.ALIGN_LEFT;
                }
            }

            if (hasLMB)
            {
                configuration.LastMarginBottom = lmb;
            }
            return(p);
        }
Exemple #10
0
        /**
         * Adds currentContent list to a paragraph element. If addNewLines is true a
         * Paragraph object is returned, else a NoNewLineParagraph object is
         * returned.
         *
         * @param currentContent IList<IElement> of the current elements to be added.
         * @param addNewLines bool to declare which paragraph element should be
         *            returned, true if new line should be added or not.
         * @param applyCSS true if CSS should be applied on the paragraph
         * @param tag the relevant tag
         * @return a List with paragraphs
         */

        public virtual IList <IElement> CurrentContentToParagraph(IList <IElement> currentContent,
                                                                  bool addNewLines, bool applyCSS, Tag tag,
                                                                  IWorkerContext ctx)
        {
            try {
                int direction         = GetRunDirection(tag);
                IList <IElement> list = new List <IElement>();
                if (currentContent.Count > 0)
                {
                    if (addNewLines)
                    {
                        Paragraph p = CreateParagraph();
                        p.MultipliedLeading = 1.2f;
                        foreach (IElement e in currentContent)
                        {
                            if (e is LineSeparator)
                            {
                                try {
                                    HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                                    Chunk newLine = (Chunk)GetCssAppliers().Apply(new Chunk(Chunk.NEWLINE), tag, htmlPipelineContext);
                                    p.Add(newLine);
                                }
                                catch (NoCustomContextException exc) {
                                    throw new RuntimeWorkerException(
                                              LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), exc);
                                }
                            }
                            p.Add(e);
                        }
                        if (p.Trim())
                        {
                            if (applyCSS)
                            {
                                p = (Paragraph)GetCssAppliers().Apply(p, tag, GetHtmlPipelineContext(ctx));
                            }
                            if (direction.Equals(PdfWriter.RUN_DIRECTION_RTL))
                            {
                                DoRtlIndentCorrections(p);
                                InvertTextAlignForParagraph(p);
                            }
                            list.Add(p);
                        }
                    }
                    else
                    {
                        NoNewLineParagraph p = new NoNewLineParagraph(float.NaN);
                        p.MultipliedLeading = 1.2f;
                        foreach (IElement e in currentContent)
                        {
                            UpdateParagraphFontIfNeeded(p, e);
                            p.Add(e);
                        }
                        p = (NoNewLineParagraph)GetCssAppliers().Apply(p, tag, GetHtmlPipelineContext(ctx));
                        if (direction.Equals(PdfWriter.RUN_DIRECTION_RTL))
                        {
                            DoRtlIndentCorrections(p);
                            InvertTextAlignForParagraph(p);
                        }
                        list.Add(p);
                    }
                }
                return(list);
            }
            catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(
                          LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
            }
        }