/** * We only alter the handling of some endtags. */ public override void EndElement(String uri, String lname, String name) { if (myTags.ContainsKey(name)) { XmlPeer peer = (XmlPeer)myTags[name]; // we don't want the document to be close // because we are going to add a page after the xml is parsed if (IsDocumentRoot(peer.Tag)) { return; } HandleEndingTags(peer.Tag); // we want to add a paragraph after the speaker chunk if ("SPEAKER".Equals(name)) { try { ITextElementArray previous = (ITextElementArray)stack.Pop(); previous.Add(new Paragraph(16)); stack.Push(previous); } catch { } } } else { HandleEndingTags(name); } }
// --------------------------------------------------------------------------- /** * replace XmlHandler.flushStack() from Java example * Flushes the stack, adding al objects in it to the document. */ private void FlushStack() { while (stack.Count > 0) { IElement element = (IElement)stack.Pop(); if (stack.Count > 0) { ITextElementArray previous = (ITextElementArray)stack.Pop(); previous.Add(element); stack.Push(previous); } else { document.Add(element); } } }
/** * Flushes the current paragraph, indicating that we're starting * a new block. * If the stack is empty, the paragraph is added to the document. * Otherwise the Paragraph is added to the stack. * @since 5.0.6 */ public void CarriageReturn() { if (currentParagraph == null) { return; } if (stack.Count == 0) { document.Add(currentParagraph); } else { IElement obj = stack.Pop(); if (obj is ITextElementArray) { ITextElementArray current = (ITextElementArray)obj; current.Add(currentParagraph); } stack.Push(obj); } currentParagraph = null; }
public virtual void EndElement(String tag) { if (!tagsSupported.ContainsKey(tag)) { return; } String follow = (String)FactoryProperties.followTags[tag]; if (follow != null) { cprops.RemoveChain(follow); return; } if (tag.Equals("font") || tag.Equals("span")) { cprops.RemoveChain(tag); return; } if (tag.Equals("a")) { if (currentParagraph == null) { currentParagraph = new Paragraph(); } IALink i = null; bool skip = false; if (interfaceProps != null) { i = (IALink)interfaceProps["alink_interface"]; if (i != null) { skip = i.Process(currentParagraph, cprops); } } if (!skip) { String href = cprops["href"]; if (href != null) { ArrayList chunks = currentParagraph.Chunks; for (int k = 0; k < chunks.Count; ++k) { Chunk ck = (Chunk)chunks[k]; ck.SetAnchor(href); } } } Paragraph tmp = (Paragraph)stack.Pop(); Phrase tmp2 = new Phrase(); tmp2.Add(currentParagraph); tmp.Add(tmp2); currentParagraph = tmp; cprops.RemoveChain("a"); return; } if (tag.Equals("br")) { return; } if (currentParagraph != null) { if (stack.Count == 0) { document.Add(currentParagraph); } else { Object obj = stack.Pop(); if (obj is ITextElementArray) { ITextElementArray current = (ITextElementArray)obj; current.Add(currentParagraph); } stack.Push(obj); } } currentParagraph = null; if (tag.Equals(HtmlTags.UNORDEREDLIST) || tag.Equals(HtmlTags.ORDEREDLIST)) { if (pendingLI) { EndElement(HtmlTags.LISTITEM); } skipText = false; cprops.RemoveChain(tag); if (stack.Count == 0) { return; } Object obj = stack.Pop(); if (!(obj is List)) { stack.Push(obj); return; } if (stack.Count == 0) { document.Add((IElement)obj); } else { ((ITextElementArray)stack.Peek()).Add(obj); } return; } if (tag.Equals(HtmlTags.LISTITEM)) { pendingLI = false; skipText = true; cprops.RemoveChain(tag); if (stack.Count == 0) { return; } Object obj = stack.Pop(); if (!(obj is ListItem)) { stack.Push(obj); return; } if (stack.Count == 0) { document.Add((IElement)obj); return; } Object list = stack.Pop(); if (!(list is List)) { stack.Push(list); return; } ListItem item = (ListItem)obj; ((List)list).Add(item); ArrayList cks = item.Chunks; if (cks.Count > 0) { item.ListSymbol.Font = ((Chunk)cks[0]).Font; } stack.Push(list); return; } if (tag.Equals("div") || tag.Equals("body")) { cprops.RemoveChain(tag); return; } if (tag.Equals(HtmlTags.PRE)) { cprops.RemoveChain(tag); isPRE = false; return; } if (tag.Equals("p")) { cprops.RemoveChain(tag); return; } if (tag.Equals("h1") || tag.Equals("h2") || tag.Equals("h3") || tag.Equals("h4") || tag.Equals("h5") || tag.Equals("h6")) { cprops.RemoveChain(tag); return; } if (tag.Equals("table")) { if (pendingTR) { EndElement("tr"); } cprops.RemoveChain("table"); IncTable table = (IncTable)stack.Pop(); PdfPTable tb = table.BuildTable(); tb.SplitRows = true; if (stack.Count == 0) { document.Add(tb); } else { ((ITextElementArray)stack.Peek()).Add(tb); } bool[] state = (bool[])tableState.Pop(); pendingTR = state[0]; pendingTD = state[1]; skipText = false; return; } if (tag.Equals("tr")) { if (pendingTD) { EndElement("td"); } pendingTR = false; cprops.RemoveChain("tr"); ArrayList cells = new ArrayList(); IncTable table = null; while (true) { Object obj = stack.Pop(); if (obj is IncCell) { cells.Add(((IncCell)obj).Cell); } if (obj is IncTable) { table = (IncTable)obj; break; } } table.AddCols(cells); table.EndRow(); stack.Push(table); skipText = true; return; } if (tag.Equals("td") || tag.Equals("th")) { pendingTD = false; cprops.RemoveChain("td"); skipText = true; return; } }
/// <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; } try { // 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 == 0) { if (cell.Colspan == 1 && cellWidths[j] == 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] != 0) { left += widths[i]; cellWidths[i] = widths[i]; } } if (100.0 >= total) { for (int i = 0; i < widths.Length; i++) { if (cellWidths[i] == 0 && widths[i] != 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; } } catch (DocumentException de) { throw de; } }