/** * Adds an element. Elements supported are <CODE>Paragraph</CODE>, * <CODE>List</CODE>, <CODE>PdfPTable</CODE>, <CODE>Image</CODE> and * <CODE>Graphic</CODE>. * <p> * It removes all the text placed with <CODE>addText()</CODE>. * @param element the <CODE>Element</CODE> */ public void AddElement(IElement element) { if (element == null) return; if (element is Image) { Image img = (Image)element; PdfPTable t = new PdfPTable(1); float w = img.WidthPercentage; if (w == 0) { t.TotalWidth = img.ScaledWidth; t.LockedWidth = true; } else t.WidthPercentage = w; t.SpacingAfter = img.SpacingAfter; t.SpacingBefore = img.SpacingBefore; switch (img.Alignment) { case Image.LEFT_ALIGN: t.HorizontalAlignment = Element.ALIGN_LEFT; break; case Image.RIGHT_ALIGN: t.HorizontalAlignment = Element.ALIGN_RIGHT; break; default: t.HorizontalAlignment = Element.ALIGN_CENTER; break; } PdfPCell c = new PdfPCell(img, true); c.Padding = 0; c.Border = img.Border; c.BorderColor = img.BorderColor; c.BorderWidth = img.BorderWidth; c.BackgroundColor = img.BackgroundColor; t.AddCell(c); element = t; } if (element.Type == Element.CHUNK) { element = new Paragraph((Chunk)element); } else if (element.Type == Element.PHRASE) { element = new Paragraph((Phrase)element); } if (element is SimpleTable) { try { element = ((SimpleTable)element).CreatePdfPTable(); } catch (DocumentException) { throw new ArgumentException("Element not allowed."); } } else if (element.Type != Element.PARAGRAPH && element.Type != Element.LIST && element.Type != Element.PTABLE && element.Type != Element.YMARK) throw new ArgumentException("Element not allowed."); if (!composite) { composite = true; compositeElements = new ArrayList(); bidiLine = null; waitPhrase = null; } compositeElements.Add(element); }
/** * Creates a PdfPTable object based on this TableAttributes object. * @return a com.lowagie.text.pdf.PdfPTable object * @throws DocumentException */ public PdfPTable CreatePdfPTable() { if (content.Count == 0) throw new BadElementException("Trying to create a table without rows."); SimpleCell rowx = (SimpleCell)content[0]; int columns = 0; foreach (SimpleCell cell in rowx.Content) { columns += cell.Colspan; } float[] widths = new float[columns]; float[] widthpercentages = new float[columns]; PdfPTable table = new PdfPTable(columns); table.TableEvent = this; table.HorizontalAlignment = alignment; int pos; foreach (SimpleCell row in content) { pos = 0; foreach (SimpleCell cell in row.Content) { if (float.IsNaN(cell.Spacing_left)) { cell.Spacing_left = cellspacing / 2f; } if (float.IsNaN(cell.Spacing_right)) { cell.Spacing_right = cellspacing / 2f; } if (float.IsNaN(cell.Spacing_top)) { cell.Spacing_top = cellspacing / 2f; } if (float.IsNaN(cell.Spacing_bottom)) { cell.Spacing_bottom = cellspacing / 2f; } cell.Padding = cellpadding; table.AddCell(cell.CreatePdfPCell(row)); if (cell.Colspan == 1) { if (cell.Width > 0) widths[pos] = cell.Width; if (cell.Widthpercentage > 0) widthpercentages[pos] = cell.Widthpercentage; } pos += cell.Colspan; } } float sumWidths = 0f; for (int i = 0; i < columns; i++) { if (widths[i] == 0) { sumWidths = 0; break; } sumWidths += widths[i]; } if (sumWidths > 0) { table.TotalWidth = sumWidths; table.SetWidths(widths); } else { for (int i = 0; i < columns; i++) { if (widthpercentages[i] == 0) { sumWidths = 0; break; } sumWidths += widthpercentages[i]; } if (sumWidths > 0) { table.SetWidths(widthpercentages); } } if (width > 0) { table.TotalWidth = width; } if (widthpercentage > 0) { table.WidthPercentage = widthpercentage; } return table; }
public PdfPTable BuildTable() { if (rows.Count == 0) return new PdfPTable(1); int ncol = 0; ArrayList c0 = (ArrayList)rows[0]; for (int k = 0; k < c0.Count; ++k) { ncol += ((PdfPCell)c0[k]).Colspan; } PdfPTable table = new PdfPTable(ncol); String width = (String)props["width"]; if (width == null) table.WidthPercentage = 100; else { if (width.EndsWith("%")) table.WidthPercentage = float.Parse(width.Substring(0, width.Length - 1), NumberFormatInfo.InvariantInfo); else { table.TotalWidth = float.Parse(width, NumberFormatInfo.InvariantInfo); table.LockedWidth = true; } } for (int row = 0; row < rows.Count; ++row) { ArrayList col = (ArrayList)rows[row]; for (int k = 0; k < col.Count; ++k) { table.AddCell((PdfPCell)col[k]); } } return table; }
/** * Create a PdfPTable based on this Table object. * @return a PdfPTable object * @throws BadElementException */ public PdfPTable CreatePdfPTable() { if (!convert2pdfptable) { throw new BadElementException("No error, just an old style table"); } AutoFillEmptyCells = true; Complete(); PdfPTable pdfptable = new PdfPTable(widths); pdfptable.ElementComplete = complete; if (NotAddedYet) pdfptable.SkipFirstHeader = true; SimpleTable t_evt = new SimpleTable(); t_evt.CloneNonPositionParameters(this); t_evt.Cellspacing = cellspacing; pdfptable.TableEvent = t_evt; pdfptable.HeaderRows = lastHeaderRow + 1; pdfptable.SplitLate = cellsFitPage; pdfptable.KeepTogether = tableFitsPage; if (!float.IsNaN(offset)) { pdfptable.SpacingBefore = offset; } pdfptable.HorizontalAlignment = alignment; if (locked) { pdfptable.TotalWidth = width; pdfptable.LockedWidth = true; } else { pdfptable.WidthPercentage = width; } foreach (Row row in this) { IElement cell; PdfPCell pcell; for (int i = 0; i < row.Columns; i++) { if ((cell = (IElement)row.GetCell(i)) != null) { if (cell is Table) { pcell = new PdfPCell(((Table)cell).CreatePdfPTable()); } else if (cell is Cell) { pcell = ((Cell)cell).CreatePdfPCell(); pcell.Padding = cellpadding + cellspacing / 2f; SimpleCell c_evt = new SimpleCell(SimpleCell.CELL); c_evt.CloneNonPositionParameters((Cell)cell); c_evt.Spacing = cellspacing * 2f; pcell.CellEvent = c_evt; } else { pcell = new PdfPCell(); } pdfptable.AddCell(pcell); } } } return pdfptable; }
/** * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. * * @param element the element to add * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not. * @throws DocumentException when a document isn't open yet, or has been closed */ public override bool Add(IElement element) { if (writer != null && writer.IsPaused()) { return false; } switch (element.Type) { // Information (headers) case Element.HEADER: info.Addkey(((Meta)element).Name, ((Meta)element).Content); break; case Element.TITLE: info.AddTitle(((Meta)element).Content); break; case Element.SUBJECT: info.AddSubject(((Meta)element).Content); break; case Element.KEYWORDS: info.AddKeywords(((Meta)element).Content); break; case Element.AUTHOR: info.AddAuthor(((Meta)element).Content); break; case Element.CREATOR: info.AddCreator(((Meta)element).Content); break; case Element.PRODUCER: // you can not change the name of the producer info.AddProducer(); break; case Element.CREATIONDATE: // you can not set the creation date, only reset it info.AddCreationDate(); break; // content (text) case Element.CHUNK: { // if there isn't a current line available, we make one if (line == null) { CarriageReturn(); } // we cast the element to a chunk PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction); // we try to add the chunk to the line, until we succeed { PdfChunk overflow; while ((overflow = line.Add(chunk)) != null) { CarriageReturn(); chunk = overflow; chunk.TrimFirstSpace(); } } pageEmpty = false; if (chunk.IsAttribute(Chunk.NEWPAGE)) { NewPage(); } break; } case Element.ANCHOR: { leadingCount++; Anchor anchor = (Anchor) element; String url = anchor.Reference; leading = anchor.Leading; if (url != null) { anchorAction = new PdfAction(url); } // we process the element element.Process(this); anchorAction = null; leadingCount--; break; } case Element.ANNOTATION: { if (line == null) { CarriageReturn(); } Annotation annot = (Annotation) element; Rectangle rect = new Rectangle(0, 0); if (line != null) rect = new Rectangle(annot.GetLlx(IndentRight - line.WidthLeft), annot.GetLly(IndentTop - currentHeight), annot.GetUrx(IndentRight - line.WidthLeft + 20), annot.GetUry(IndentTop - currentHeight - 20)); PdfAnnotation an = PdfAnnotationsImp.ConvertAnnotation(writer, annot, rect); annotationsImp.AddPlainAnnotation(an); pageEmpty = false; break; } case Element.PHRASE: { leadingCount++; // we cast the element to a phrase and set the leading of the document leading = ((Phrase) element).Leading; // we process the element element.Process(this); leadingCount--; break; } case Element.PARAGRAPH: { leadingCount++; // we cast the element to a paragraph Paragraph paragraph = (Paragraph) element; AddSpacing(paragraph.SpacingBefore, leading, paragraph.Font); // we adjust the parameters of the document alignment = paragraph.Alignment; leading = paragraph.TotalLeading; CarriageReturn(); // we don't want to make orphans/widows if (currentHeight + line.Height + leading > IndentTop - IndentBottom) { NewPage(); } indentation.indentLeft += paragraph.IndentationLeft; indentation.indentRight += paragraph.IndentationRight; CarriageReturn(); IPdfPageEvent pageEvent = writer.PageEvent; if (pageEvent != null && !isSectionTitle) pageEvent.OnParagraph(writer, this, IndentTop - currentHeight); // if a paragraph has to be kept together, we wrap it in a table object if (paragraph.KeepTogether) { CarriageReturn(); PdfPTable table = new PdfPTable(1); table.WidthPercentage = 100f; PdfPCell cell = new PdfPCell(); cell.AddElement(paragraph); cell.Border = Rectangle.NO_BORDER; cell.Padding = 0; table.AddCell(cell); indentation.indentLeft -= paragraph.IndentationLeft; indentation.indentRight -= paragraph.IndentationRight; this.Add(table); indentation.indentLeft += paragraph.IndentationLeft; indentation.indentRight += paragraph.IndentationRight; } else { line.SetExtraIndent(paragraph.FirstLineIndent); element.Process(this); CarriageReturn(); AddSpacing(paragraph.SpacingAfter, paragraph.TotalLeading, paragraph.Font); } if (pageEvent != null && !isSectionTitle) pageEvent.OnParagraphEnd(writer, this, IndentTop - currentHeight); alignment = Element.ALIGN_LEFT; indentation.indentLeft -= paragraph.IndentationLeft; indentation.indentRight -= paragraph.IndentationRight; CarriageReturn(); leadingCount--; break; } case Element.SECTION: case Element.CHAPTER: { // Chapters and Sections only differ in their constructor // so we cast both to a Section Section section = (Section) element; IPdfPageEvent pageEvent = writer.PageEvent; bool hasTitle = section.NotAddedYet && section.Title != null; // if the section is a chapter, we begin a new page if (section.TriggerNewPage) { NewPage(); } if (hasTitle) { float fith = IndentTop - currentHeight; int rotation = pageSize.Rotation; if (rotation == 90 || rotation == 180) fith = pageSize.Height - fith; PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith); while (currentOutline.Level >= section.Depth) { currentOutline = currentOutline.Parent; } PdfOutline outline = new PdfOutline(currentOutline, destination, section.GetBookmarkTitle(), section.BookmarkOpen); currentOutline = outline; } // some values are set CarriageReturn(); indentation.sectionIndentLeft += section.IndentationLeft; indentation.sectionIndentRight += section.IndentationRight; if (section.NotAddedYet && pageEvent != null) if (element.Type == Element.CHAPTER) pageEvent.OnChapter(writer, this, IndentTop - currentHeight, section.Title); else pageEvent.OnSection(writer, this, IndentTop - currentHeight, section.Depth, section.Title); // the title of the section (if any has to be printed) if (hasTitle) { isSectionTitle = true; Add(section.Title); isSectionTitle = false; } indentation.sectionIndentLeft += section.Indentation; // we process the section element.Process(this); // some parameters are set back to normal again indentation.sectionIndentLeft -= (section.IndentationLeft + section.Indentation); indentation.sectionIndentRight -= section.IndentationRight; if (section.ElementComplete && pageEvent != null) if (element.Type == Element.CHAPTER) pageEvent.OnChapterEnd(writer, this, IndentTop - currentHeight); else pageEvent.OnSectionEnd(writer, this, IndentTop - currentHeight); break; } case Element.LIST: { // we cast the element to a List List list = (List) element; if (list.Alignindent) { list.NormalizeIndentation(); } // we adjust the document indentation.listIndentLeft += list.IndentationLeft; indentation.indentRight += list.IndentationRight; // we process the items in the list element.Process(this); // some parameters are set back to normal again indentation.listIndentLeft -= list.IndentationLeft; indentation.indentRight -= list.IndentationRight; CarriageReturn(); break; } case Element.LISTITEM: { leadingCount++; // we cast the element to a ListItem ListItem listItem = (ListItem) element; AddSpacing(listItem.SpacingBefore, leading, listItem.Font); // we adjust the document alignment = listItem.Alignment; indentation.listIndentLeft += listItem.IndentationLeft; indentation.indentRight += listItem.IndentationRight; leading = listItem.TotalLeading; CarriageReturn(); // we prepare the current line to be able to show us the listsymbol line.ListItem = listItem; // we process the item element.Process(this); AddSpacing(listItem.SpacingAfter, listItem.TotalLeading, listItem.Font); // if the last line is justified, it should be aligned to the left if (line.HasToBeJustified()) { line.ResetAlignment(); } // some parameters are set back to normal again CarriageReturn(); indentation.listIndentLeft -= listItem.IndentationLeft; indentation.indentRight -= listItem.IndentationRight; leadingCount--; break; } case Element.RECTANGLE: { Rectangle rectangle = (Rectangle) element; graphics.Rectangle(rectangle); pageEmpty = false; break; } case Element.PTABLE: { PdfPTable ptable = (PdfPTable)element; if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines EnsureNewLine(); FlushLines(); AddPTable(ptable); pageEmpty = false; NewLine(); break; } case Element.MULTI_COLUMN_TEXT: { EnsureNewLine(); FlushLines(); MultiColumnText multiText = (MultiColumnText) element; float height = multiText.Write(writer.DirectContent, this, IndentTop - currentHeight); currentHeight += height; text.MoveText(0, -1f* height); pageEmpty = false; break; } case Element.TABLE : { if (element is SimpleTable) { PdfPTable ptable = ((SimpleTable)element).CreatePdfPTable(); if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines EnsureNewLine(); FlushLines(); AddPTable(ptable); pageEmpty = false; break; } else if (element is Table) { try { PdfPTable ptable = ((Table)element).CreatePdfPTable(); if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines EnsureNewLine(); FlushLines(); AddPTable(ptable); pageEmpty = false; break; } catch (BadElementException) { // constructing the PdfTable // Before the table, add a blank line using offset or default leading float offset = ((Table)element).Offset; if (float.IsNaN(offset)) offset = leading; CarriageReturn(); lines.Add(new PdfLine(IndentLeft, IndentRight, alignment, offset)); currentHeight += offset; AddPdfTable((Table)element); } } else { return false; } break; } case Element.JPEG: case Element.JPEG2000: case Element.JBIG2: case Element.IMGRAW: case Element.IMGTEMPLATE: { //carriageReturn(); suggestion by Marc Campforts Add((Image) element); break; } case Element.YMARK: { IDrawInterface zh = (IDrawInterface)element; zh.Draw(graphics, IndentLeft, IndentBottom, IndentRight, IndentTop, IndentTop - currentHeight - (leadingCount > 0 ? leading : 0)); pageEmpty = false; break; } case Element.MARKED: { MarkedObject mo; if (element is MarkedSection) { mo = ((MarkedSection)element).Title; if (mo != null) { mo.Process(this); } } mo = (MarkedObject)element; mo.Process(this); break; } default: return false; } lastElementType = element.Type; return true; }