private static void AddPage( IDocListener document, PdfContentByte canvas, Rectangle documentRectangle, BaseColor backgroundColor) { document.NewPage(); TextSharpHelpers.DrawRectangle(canvas, documentRectangle, backgroundColor); }
/// <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> /// <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(); } } }