示例#1
0
 public virtual void EndDocument()
 {
     foreach (IElement e in stack)
     {
         document.Add(e);
     }
     if (currentParagraph != null)
     {
         document.Add(currentParagraph);
     }
     currentParagraph = null;
 }
示例#2
0
        /// <summary>
        /// Commit the current table and reset state
        /// </summary>
        /// <param name="state">
        /// The state
        /// </param>
        /// <param name="doc">
        /// The pdf document to commit the current table
        /// </param>
        private static void CommitTable(RendererState state, IDocListener doc)
        {
            if (state.Table != null)
            {
                PopulateTable(state);

                // containerTable.Write(writer);
                PdfPTable table = CreatePdf(state.Table);
                if (table != null)
                {
                    doc.Add(table);
                    table.DeleteBodyRows();
                }

                doc.NewPage();
                state.Reset();
            }
        }
示例#3
0
 /**
  * @see com.itextpdf.text.xml.simpleparser.SimpleXMLDocHandler#endDocument()
  */
 public virtual void EndDocument()
 {
     // flush the stack
     foreach (IElement e in stack)
     {
         document.Add(e);
     }
     // add current paragraph
     if (currentParagraph != null)
     {
         document.Add(currentParagraph);
     }
     currentParagraph = null;
 }
        /// <summary>
        /// This method deals with the starting tags.
        /// </summary>
        /// <param name="name">the name of the tag</param>
        /// <param name="attributes">the list of attributes</param>
        public void HandleStartingTags(String name, Properties attributes)
        {
            //System.err.Println("Start: " + name);
            if (ignore || ElementTags.IGNORE.Equals(name))
            {
                ignore = true;
                return;
            }

            // maybe there is some meaningful data that wasn't between tags
            if (currentChunk != null)
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                }
                catch {
                    if (bf == null)
                    {
                        current = new Paragraph("", new Font());
                    }
                    else
                    {
                        current = new Paragraph("", new Font(this.bf));
                    }
                }
                current.Add(currentChunk);
                stack.Push(current);
                currentChunk = null;
            }

            // registerfont
            if (name.Equals("registerfont"))
            {
                FontFactory.Register(attributes);
            }

            // header
            if (ElementTags.HEADER.Equals(name))
            {
                stack.Push(new HeaderFooter(attributes));
                return;
            }

            // footer
            if (ElementTags.FOOTER.Equals(name))
            {
                stack.Push(new HeaderFooter(attributes));
                return;
            }

            // before
            if (name.Equals("before"))
            {
                HeaderFooter tmp = (HeaderFooter)stack.Pop();

                tmp.Before = ElementFactory.GetPhrase(attributes);
                stack.Push(tmp);
                return;
            }

            // after
            if (name.Equals("after"))
            {
                HeaderFooter tmp = (HeaderFooter)stack.Pop();

                tmp.After = ElementFactory.GetPhrase(attributes);
                stack.Push(tmp);
                return;
            }

            // chunks
            if (ElementTags.CHUNK.Equals(name))
            {
                currentChunk = ElementFactory.GetChunk(attributes);
                if (bf != null)
                {
                    currentChunk.Font = new Font(this.bf);
                }
                return;
            }

            // symbols
            if (ElementTags.ENTITY.Equals(name))
            {
                Font f = new Font();
                if (currentChunk != null)
                {
                    HandleEndingTags(ElementTags.CHUNK);
                    f = currentChunk.Font;
                }
                currentChunk = EntitiesToSymbol.Get(attributes[ElementTags.ID], f);
                return;
            }

            // phrases
            if (ElementTags.PHRASE.Equals(name))
            {
                stack.Push(ElementFactory.GetPhrase(attributes));
                return;
            }

            // anchors
            if (ElementTags.ANCHOR.Equals(name))
            {
                stack.Push(ElementFactory.GetAnchor(attributes));
                return;
            }

            // paragraphs and titles
            if (ElementTags.PARAGRAPH.Equals(name) || ElementTags.TITLE.Equals(name))
            {
                stack.Push(ElementFactory.GetParagraph(attributes));
                return;
            }

            // lists
            if (ElementTags.LIST.Equals(name))
            {
                stack.Push(ElementFactory.GetList(attributes));
                return;
            }

            // listitems
            if (ElementTags.LISTITEM.Equals(name))
            {
                stack.Push(ElementFactory.GetListItem(attributes));
                return;
            }

            // cells
            if (ElementTags.CELL.Equals(name))
            {
                stack.Push(ElementFactory.GetCell(attributes));
                return;
            }

            // tables
            if (ElementTags.TABLE.Equals(name))
            {
                Table   table  = ElementFactory.GetTable(attributes);
                float[] widths = table.ProportionalWidths;
                for (int i = 0; i < widths.Length; i++)
                {
                    if (widths[i] == 0)
                    {
                        widths[i] = 100.0f / (float)widths.Length;
                    }
                }
                try {
                    table.Widths = widths;
                }
                catch (BadElementException bee) {
                    // this shouldn't happen
                    throw new Exception("", bee);
                }
                stack.Push(table);
                return;
            }

            // sections
            if (ElementTags.SECTION.Equals(name))
            {
                IElement previous = (IElement)stack.Pop();
                Section  section;
                section = ElementFactory.GetSection((Section)previous, attributes);
                stack.Push(previous);
                stack.Push(section);
                return;
            }

            // chapters
            if (ElementTags.CHAPTER.Equals(name))
            {
                stack.Push(ElementFactory.GetChapter(attributes));
                return;
            }

            // images
            if (ElementTags.IMAGE.Equals(name))
            {
                try {
                    Image img = ElementFactory.GetImage(attributes);
                    try {
                        AddImage(img);
                        return;
                    }
                    catch {
                        // if there is no element on the stack, the Image is added to the document
                        try {
                            document.Add(img);
                        }
                        catch (DocumentException de) {
                            throw new Exception("", de);
                        }
                        return;
                    }
                }
                catch (Exception e) {
                    throw new Exception("", e);
                }
            }

            // annotations
            if (ElementTags.ANNOTATION.Equals(name))
            {
                Annotation        annotation = ElementFactory.GetAnnotation(attributes);
                ITextElementArray current;
                try {
                    try {
                        current = (ITextElementArray)stack.Pop();
                        try {
                            current.Add(annotation);
                        }
                        catch {
                            document.Add(annotation);
                        }
                        stack.Push(current);
                    }
                    catch {
                        document.Add(annotation);
                    }
                    return;
                }
                catch (DocumentException de) {
                    throw de;
                }
            }

            // newlines
            if (IsNewline(name))
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                    current.Add(Chunk.NEWLINE);
                    stack.Push(current);
                }
                catch {
                    if (currentChunk == null)
                    {
                        try {
                            document.Add(Chunk.NEWLINE);
                        }
                        catch (DocumentException de) {
                            throw de;
                        }
                    }
                    else
                    {
                        currentChunk.Append("\n");
                    }
                }
                return;
            }

            // newpage
            if (IsNewpage(name))
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                    Chunk newPage = new Chunk("");
                    newPage.SetNewPage();
                    if (bf != null)
                    {
                        newPage.Font = new Font(this.bf);
                    }
                    current.Add(newPage);
                    stack.Push(current);
                }
                catch {
                    document.NewPage();
                }
                return;
            }

            if (ElementTags.HORIZONTALRULE.Equals(name))
            {
                ITextElementArray current;
                LineSeparator     hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);
                try {
                    current = (ITextElementArray)stack.Pop();
                    current.Add(hr);
                    stack.Push(current);
                } catch (InvalidOperationException) {
                    document.Add(hr);
                }
                return;
            }

            // documentroot
            if (IsDocumentRoot(name))
            {
                String value;
                // pagesize and orientation specific code suggested by Samuel Gabriel
                // Updated by Ricardo Coutinho. Only use if set in html!
                Rectangle pageSize    = null;
                String    orientation = null;
                foreach (string key in attributes.Keys)
                {
                    value = attributes[key];
                    // margin specific code suggested by Reza Nasiri
                    if (Util.EqualsIgnoreCase(ElementTags.LEFT, key))
                    {
                        leftMargin = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
                    }
                    if (Util.EqualsIgnoreCase(ElementTags.RIGHT, key))
                    {
                        rightMargin = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
                    }
                    if (Util.EqualsIgnoreCase(ElementTags.TOP, key))
                    {
                        topMargin = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
                    }
                    if (Util.EqualsIgnoreCase(ElementTags.BOTTOM, key))
                    {
                        bottomMargin = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
                    }
                    if (ElementTags.PAGE_SIZE.Equals(key))
                    {
                        pageSize = (Rectangle)typeof(PageSize).GetField(value).GetValue(null);
                    }
                    else if (ElementTags.ORIENTATION.Equals(key))
                    {
                        if ("landscape".Equals(value))
                        {
                            orientation = "landscape";
                        }
                    }
                    else
                    {
                        document.Add(new Meta(key, value));
                    }
                }
                if (pageSize != null)
                {
                    if ("landscape".Equals(orientation))
                    {
                        pageSize = pageSize.Rotate();
                    }
                    document.SetPageSize(pageSize);
                }
                document.SetMargins(leftMargin, rightMargin, topMargin,
                                    bottomMargin);
                if (controlOpenClose)
                {
                    document.Open();
                }
            }
        }
示例#5
0
        /// <summary>
        /// Commit the current table and reset state
        /// </summary>
        /// <param name="state">
        /// The state
        /// </param>
        /// <param name="doc">
        /// The pdf document to commit the current table
        /// </param>
        private static void CommitTable(RendererState state, IDocListener doc)
        {
            if (state.Table != null)
            {
                PopulateTable(state);

                // containerTable.Write(writer);
                PdfPTable table = CreatePdf(state.Table);
                if (table != null)
                {
                    doc.Add(table);
                    table.DeleteBodyRows();
                }

                doc.NewPage();
                state.Reset();
            }
        }
        /// <summary>
        /// This method deals with the starting tags.
        /// </summary>
        /// <param name="name">the name of the tag</param>
        public void HandleEndingTags(string name)
        {
            //System.err.Println("Stop: " + name);

            if (ElementTags.IGNORE.Equals(name))
            {
                Ignore = false;
                return;
            }
            if (Ignore)
            {
                return;
            }
            // tags that don't have any content
            if (isNewpage(name) || ElementTags.ANNOTATION.Equals(name) || ElementTags.IMAGE.Equals(name) || isNewline(name))
            {
                return;
            }

            // titles of sections and chapters
            if (ElementTags.TITLE.Equals(name))
            {
                Paragraph current = (Paragraph)Stack.Pop();
                if (CurrentChunk != null)
                {
                    current.Add(CurrentChunk);
                    CurrentChunk = null;
                }
                Section previous = (Section)Stack.Pop();
                previous.Title = current;
                Stack.Push(previous);
                return;
            }

            // all other endtags
            if (CurrentChunk != null)
            {
                ITextElementArray current;
                try
                {
                    current = (ITextElementArray)Stack.Pop();
                }
                catch
                {
                    current = new Paragraph();
                }
                current.Add(CurrentChunk);
                Stack.Push(current);
                CurrentChunk = null;
            }

            // chunks
            if (ElementTags.CHUNK.Equals(name))
            {
                return;
            }

            // phrases, anchors, lists, tables
            if (ElementTags.PHRASE.Equals(name) || ElementTags.ANCHOR.Equals(name) || ElementTags.LIST.Equals(name) ||
                ElementTags.PARAGRAPH.Equals(name))
            {
                IElement current = (IElement)Stack.Pop();
                try
                {
                    ITextElementArray previous = (ITextElementArray)Stack.Pop();
                    previous.Add(current);
                    Stack.Push(previous);
                }
                catch
                {
                    Document.Add(current);
                }
                return;
            }

            // listitems
            if (ElementTags.LISTITEM.Equals(name))
            {
                ListItem listItem = (ListItem)Stack.Pop();
                List     list     = (List)Stack.Pop();
                list.Add(listItem);
                Stack.Push(list);
            }

            // tables
            if (ElementTags.TABLE.Equals(name))
            {
                Table table = (Table)Stack.Pop();
                try
                {
                    ITextElementArray previous = (ITextElementArray)Stack.Pop();
                    previous.Add(table);
                    Stack.Push(previous);
                }
                catch
                {
                    Document.Add(table);
                }
                return;
            }

            // rows
            if (ElementTags.ROW.Equals(name))
            {
                ArrayList cells   = new ArrayList();
                int       columns = 0;
                Table     table;
                Cell      cell;
                while (true)
                {
                    IElement element = (IElement)Stack.Pop();
                    if (element.Type == Element.CELL)
                    {
                        cell     = (Cell)element;
                        columns += cell.Colspan;
                        cells.Add(cell);
                    }
                    else
                    {
                        table = (Table)element;
                        break;
                    }
                }
                if (table.Columns < columns)
                {
                    table.AddColumns(columns - table.Columns);
                }
                cells.Reverse(0, cells.Count);
                string  width;
                float[] cellWidths = new float[columns];
                bool[]  cellNulls  = new bool[columns];
                for (int i = 0; i < columns; i++)
                {
                    cellWidths[i] = 0;
                    cellNulls[i]  = true;
                }
                float total = 0;
                int   j     = 0;
                foreach (Cell c in cells)
                {
                    cell  = c;
                    width = cell.GetWidthAsString();
                    if (cell.Width.ApproxEquals(0))
                    {
                        if (cell.Colspan == 1 && cellWidths[j].ApproxEquals(0))
                        {
                            try
                            {
                                cellWidths[j] = 100f / columns;
                                total        += cellWidths[j];
                            }
                            catch
                            {
                                // empty on purpose
                            }
                        }
                        else if (cell.Colspan == 1)
                        {
                            cellNulls[j] = false;
                        }
                    }
                    else if (cell.Colspan == 1 && width.EndsWith("%"))
                    {
                        try
                        {
                            cellWidths[j] = float.Parse(width.Substring(0, width.Length - 1), System.Globalization.NumberFormatInfo.InvariantInfo);
                            total        += cellWidths[j];
                        }
                        catch
                        {
                            // empty on purpose
                        }
                    }
                    j += cell.Colspan;
                    table.AddCell(cell);
                }
                float[] widths = table.ProportionalWidths;
                if (widths.Length == columns)
                {
                    float left = 0.0f;
                    for (int i = 0; i < columns; i++)
                    {
                        if (cellNulls[i] && widths[i].ApproxNotEqual(0))
                        {
                            left         += widths[i];
                            cellWidths[i] = widths[i];
                        }
                    }
                    if (100.0 >= total)
                    {
                        for (int i = 0; i < widths.Length; i++)
                        {
                            if (cellWidths[i].ApproxEquals(0) && widths[i].ApproxNotEqual(0))
                            {
                                cellWidths[i] = (widths[i] / left) * (100.0f - total);
                            }
                        }
                    }
                    table.Widths = cellWidths;
                }
                Stack.Push(table);
            }

            // registerfont
            if (name.Equals("registerfont"))
            {
                return;
            }

            // header
            if (ElementTags.HEADER.Equals(name))
            {
                Document.Header = (HeaderFooter)Stack.Pop();
                return;
            }

            // footer
            if (ElementTags.FOOTER.Equals(name))
            {
                Document.Footer = (HeaderFooter)Stack.Pop();
                return;
            }

            // before
            if (name.Equals("before"))
            {
                return;
            }

            // after
            if (name.Equals("after"))
            {
                return;
            }

            // cells
            if (ElementTags.CELL.Equals(name))
            {
                return;
            }

            // sections
            if (ElementTags.SECTION.Equals(name))
            {
                Stack.Pop();
                return;
            }

            // chapters
            if (ElementTags.CHAPTER.Equals(name))
            {
                Document.Add((IElement)Stack.Pop());
                return;
            }

            // the documentroot
            if (IsDocumentRoot(name))
            {
                try
                {
                    while (true)
                    {
                        IElement element = (IElement)Stack.Pop();
                        try
                        {
                            ITextElementArray previous = (ITextElementArray)Stack.Pop();
                            previous.Add(element);
                            Stack.Push(previous);
                        }
                        catch
                        {
                            Document.Add(element);
                        }
                    }
                }
                catch
                {
                    // empty on purpose
                }
                if (ControlOpenClose)
                {
                    Document.Close();
                }
                return;
            }
        }
示例#7
0
        /// <summary>
        /// This method deals with the starting tags.
        /// </summary>
        /// <param name="name">the name of the tag</param>
        /// <param name="attributes">the list of attributes</param>
        public void handleStartingTags(String name, Properties attributes)
        {
            //System.err.println("Start: " + name);
            if (ignore || ElementTags.IGNORE.Equals(name))
            {
                ignore = true;
                return;
            }

            // maybe there is some meaningful data that wasn't between tags
            if (currentChunk != null)
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                }
                catch {
                    current = new Paragraph();
                }
                current.Add(currentChunk);
                stack.Push(current);
                currentChunk = null;
            }

            // registerfont
            if (name.Equals("registerfont"))
            {
                FontFactory.register(attributes);
            }

            // header
            if (ElementTags.HEADER.Equals(name))
            {
                stack.Push(new HeaderFooter(attributes));
                return;
            }

            // footer
            if (ElementTags.FOOTER.Equals(name))
            {
                stack.Push(new HeaderFooter(attributes));
                return;
            }

            // before
            if (name.Equals("before"))
            {
                HeaderFooter tmp = (HeaderFooter)stack.Pop();

                tmp.Before = new Phrase(attributes);
                stack.Push(tmp);
                return;
            }

            // after
            if (name.Equals("after"))
            {
                HeaderFooter tmp = (HeaderFooter)stack.Pop();

                tmp.After = new Phrase(attributes);
                stack.Push(tmp);
                return;
            }

            // chunks
            if (Chunk.isTag(name))
            {
                currentChunk = new Chunk(attributes);
                return;
            }

            // symbols
            if (Entities.isTag(name))
            {
                Font f = new Font();
                if (currentChunk != null)
                {
                    handleEndingTags(ElementTags.CHUNK);
                    f = currentChunk.Font;
                }
                currentChunk = Entities.get(attributes[ElementTags.ID], f);
                return;
            }

            // phrases
            if (Phrase.isTag(name))
            {
                stack.Push(new Phrase(attributes));
                return;
            }

            // anchors
            if (Anchor.isTag(name))
            {
                stack.Push(new Anchor(attributes));
                return;
            }

            // paragraphs and titles
            if (Paragraph.isTag(name) || Section.isTitle(name))
            {
                stack.Push(new Paragraph(attributes));
                return;
            }

            // lists
            if (List.isTag(name))
            {
                stack.Push(new List(attributes));
                return;
            }

            // listitems
            if (ListItem.isTag(name))
            {
                stack.Push(new ListItem(attributes));
                return;
            }

            // cells
            if (Cell.isTag(name))
            {
                stack.Push(new Cell(attributes));
                return;
            }

            // tables
            if (Table.isTag(name))
            {
                Table   table  = new Table(attributes);
                float[] widths = table.ProportionalWidths;
                for (int i = 0; i < widths.Length; i++)
                {
                    if (widths[i] == 0)
                    {
                        widths[i] = 100.0f / (float)widths.Length;
                    }
                }
                try {
                    table.Widths = widths;
                }
                catch (BadElementException bee) {
                    // this shouldn't happen
                    throw new Exception("", bee);
                }
                stack.Push(table);
                return;
            }

            // sections
            if (Section.isTag(name))
            {
                IElement previous = (IElement)stack.Pop();
                Section  section;
                try {
                    section = ((Section)previous).addSection(attributes);
                }
                catch (Exception cce) {
                    throw cce;
                }
                stack.Push(previous);
                stack.Push(section);
                return;
            }

            // chapters
            if (Chapter.isTag(name))
            {
                String value;                 // changed after a suggestion by Serge S. Vasiljev
                if ((value = (String)attributes.Remove(ElementTags.NUMBER)) != null)
                {
                    chapters = int.Parse(value);
                }
                else
                {
                    chapters++;
                }
                Chapter chapter = new Chapter(attributes, chapters);
                stack.Push(chapter);
                return;
            }

            // images
            if (Image.isTag(name))
            {
                try {
                    Image  img = Image.getInstance(attributes);
                    Object current;
                    try {
                        // if there is an element on the stack...
                        current = stack.Pop();
                        // ...and it's a Chapter or a Section, the Image can be added directly
                        if (current is Chapter || current is Section || current is Cell)
                        {
                            ((ITextElementArray)current).Add(img);
                            stack.Push(current);
                            return;
                        }
                        // ...if not, the Image is wrapped in a Chunk before it's added
                        else
                        {
                            Stack newStack = new Stack();
                            try {
                                while (!(current is Chapter || current is Section || current is Cell))
                                {
                                    newStack.Push(current);
                                    if (current is Anchor)
                                    {
                                        img.Annotation = new Annotation(0, 0, 0, 0, ((Anchor)current).Reference);
                                    }
                                    current = stack.Pop();
                                }
                                ((ITextElementArray)current).Add(img);
                                stack.Push(current);
                            }
                            catch {
                                document.Add(img);
                            }
                            while (!(newStack.Count == 0))
                            {
                                stack.Push(newStack.Pop());
                            }
                            return;
                        }
                    }
                    catch {
                        // if there is no element on the stack, the Image is added to the document
                        try {
                            document.Add(img);
                        }
                        catch (DocumentException de) {
                            throw new Exception("", de);
                        }
                        return;
                    }
                }
                catch (Exception e) {
                    throw new Exception("", e);
                }
            }

            // annotations
            if (Annotation.isTag(name))
            {
                Annotation        annotation = new Annotation(attributes);
                ITextElementArray current;
                try {
                    try {
                        current = (ITextElementArray)stack.Pop();
                        try {
                            current.Add(annotation);
                        }
                        catch {
                            document.Add(annotation);
                        }
                        stack.Push(current);
                    }
                    catch {
                        document.Add(annotation);
                    }
                    return;
                }
                catch (DocumentException de) {
                    throw new Exception("", de);
                }
            }

            // newlines
            if (isNewline(name))
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                    current.Add(Chunk.NEWLINE);
                    stack.Push(current);
                }
                catch {
                    if (currentChunk == null)
                    {
                        try {
                            document.Add(Chunk.NEWLINE);
                        }
                        catch (DocumentException de) {
                            throw new Exception("", de);
                        }
                    }
                    else
                    {
                        currentChunk.append("\n");
                    }
                }
                return;
            }

            // newpage
            if (isNewpage(name))
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                    Chunk newPage = new Chunk("");
                    newPage.setNewPage();
                    current.Add(newPage);
                    stack.Push(current);
                }
                catch {
                    try {
                        document.newPage();
                    }
                    catch (DocumentException de) {
                        throw new Exception("", de);
                    }
                }
                return;
            }

            // newpage
            if (ElementTags.HORIZONTALRULE.Equals(name))
            {
                ITextElementArray current;
                Graphic           hr = new Graphic();
                hr.setHorizontalLine(1.0f, 100.0f);
                try {
                    current = (ITextElementArray)stack.Pop();
                    current.Add(hr);
                    stack.Push(current);
                }
                catch {
                    try {
                        document.Add(hr);
                    }
                    catch (DocumentException de) {
                        throw new Exception("", de);
                    }
                }
                return;
            }

            // documentroot
            if (isDocumentRoot(name))
            {
                String value;
                foreach (string key in attributes.Keys)
                {
                    value = attributes[key];
                    try {
                        document.Add(new Meta(key, value));
                    }
                    catch (DocumentException de) {
                        throw new Exception("", de);
                    }
                }
                if (controlOpenClose)
                {
                    document.Open();
                }
            }
        }