/// <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(); } } }
/// <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(); } } }