/** * Writes the HTML representation of a section. * * @param section the section to write * @param indent the indentation */ protected void WriteSection(Section section, int indent) { if (section.Title != null) { int depth = section.Depth - 1; if (depth > 5) { depth = 5; } Properties styleAttributes = new Properties(); if (section.Title.LeadingDefined) { styleAttributes[MarkupTags.CSS_KEY_LINEHEIGHT] = section.Title.Leading.ToString() + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.H[depth]); Write(section.Title.Font, styleAttributes); String alignment = HtmlEncoder.GetAlignment(section.Title.Alignment); if (!"".Equals(alignment)) { Write(HtmlTags.ALIGN, alignment); } if (HasMarkupAttributes(section.Title)) { WriteMarkupAttributes(section.Title); } os.WriteByte(GT); currentfont.Push(section.Title.Font); // contents foreach (IElement i in section.Title) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.H[depth]); currentfont.Pop(); } foreach (IElement i in section) { Write(i, indent); } }
/// <summary> /// Writes the HTML representation of a section. /// </summary> /// <param name="section">the section to write</param> /// <param name="indent">the indentation</param> protected void WriteSection(Section section, int indent) { if (section.Title != null) { var depth = section.Depth - 1; if (depth > 5) { depth = 5; } var styleAttributes = new Properties(); if (section.Title.HasLeading()) { styleAttributes[html.Markup.CSS_KEY_LINEHEIGHT] = section.Title.TotalLeading + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.H[depth]); Write(section.Title.Font, styleAttributes); var alignment = HtmlEncoder.GetAlignment(section.Title.Alignment); if (!"".Equals(alignment)) { Write(HtmlTags.ALIGN, alignment); } WriteMarkupAttributes(Markup); Os.WriteByte(GT); Currentfont.Push(section.Title.Font); // contents foreach (IElement i in section.Title) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.H[depth]); Currentfont.Pop(); } foreach (IElement i in section) { Write(i, indent); } }
/** * Writes the HTML representation of an element. * * @param element the element * @param indent the indentation */ protected void Write(IElement element, int indent) { Properties styleAttributes = null; switch (element.Type) { case Element.CHUNK: { Chunk chunk = (Chunk)element; // if the chunk contains an image, return the image representation Image image = chunk.GetImage(); if (image != null) { Write(image, indent); return; } if (chunk.IsEmpty()) { return; } Hashtable attributes = chunk.Attributes; if (attributes != null && attributes[Chunk.NEWPAGE] != null) { return; } // This doesn't seem to work: //if (attributes != null && attributes.Get(Chunk.SUBSUPSCRIPT) != null) { // float p = (((Float)attributes.Get(Chunk.SUBSUPSCRIPT)).FloatValue() * 100f) / chunk.Font.Size(); // styleAttributes = new Properties(); // styleAttributes.SetProperty(MarkupTags.CSS_VERTICALALIGN, "" + p + "%"); //} bool tag = IsOtherFont(chunk.Font) || HasMarkupAttributes(chunk) || styleAttributes != null; if (tag) { // start span tag AddTabs(indent); WriteStart(HtmlTags.SPAN); if (IsOtherFont(chunk.Font)) { Write(chunk.Font, styleAttributes); } if (HasMarkupAttributes(chunk)) { WriteMarkupAttributes(chunk); } os.WriteByte(GT); } if (attributes != null && attributes[Chunk.SUBSUPSCRIPT] != null) { // start sup or sub tag if ((float)attributes[Chunk.SUBSUPSCRIPT] > 0) { WriteStart(HtmlTags.SUP); } else { WriteStart(HtmlTags.SUB); } os.WriteByte(GT); } // contents Write(HtmlEncoder.Encode(chunk.Content)); if (attributes != null && attributes[Chunk.SUBSUPSCRIPT] != null) { // end sup or sub tag os.WriteByte(LT); os.WriteByte(FORWARD); if ((float)attributes[Chunk.SUBSUPSCRIPT] > 0) { Write(HtmlTags.SUP); } else { Write(HtmlTags.SUB); } os.WriteByte(GT); } if (tag) { // end tag WriteEnd(MarkupTags.HTML_TAG_SPAN); } return; } case Element.PHRASE: { Phrase phrase = (Phrase)element; styleAttributes = new Properties(); if (phrase.LeadingDefined) { styleAttributes[MarkupTags.CSS_KEY_LINEHEIGHT] = phrase.Leading.ToString() + "pt"; } // start tag AddTabs(indent); WriteStart(MarkupTags.HTML_TAG_SPAN); if (HasMarkupAttributes(phrase)) { WriteMarkupAttributes(phrase); } Write(phrase.Font, styleAttributes); os.WriteByte(GT); currentfont.Push(phrase.Font); // contents foreach (IElement i in phrase) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(MarkupTags.HTML_TAG_SPAN); currentfont.Pop(); return; } case Element.ANCHOR: { Anchor anchor = (Anchor)element; styleAttributes = new Properties(); if (anchor.LeadingDefined) { styleAttributes[MarkupTags.CSS_KEY_LINEHEIGHT] = anchor.Leading.ToString() + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.ANCHOR); if (anchor.Name != null) { Write(HtmlTags.NAME, anchor.Name); } if (anchor.Reference != null) { Write(HtmlTags.REFERENCE, anchor.Reference); } if (HasMarkupAttributes(anchor)) { WriteMarkupAttributes(anchor); } Write(anchor.Font, styleAttributes); os.WriteByte(GT); currentfont.Push(anchor.Font); // contents foreach (IElement i in anchor) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.ANCHOR); currentfont.Pop(); return; } case Element.PARAGRAPH: { Paragraph paragraph = (Paragraph)element; styleAttributes = new Properties(); if (paragraph.LeadingDefined) { styleAttributes[MarkupTags.CSS_KEY_LINEHEIGHT] = paragraph.Leading.ToString() + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.DIV); if (HasMarkupAttributes(paragraph)) { WriteMarkupAttributes(paragraph); } String alignment = HtmlEncoder.GetAlignment(paragraph.Alignment); if (!"".Equals(alignment)) { Write(HtmlTags.ALIGN, alignment); } Write(paragraph.Font, styleAttributes); os.WriteByte(GT); currentfont.Push(paragraph.Font); // contents foreach (IElement i in paragraph) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.DIV); currentfont.Pop(); return; } case Element.SECTION: case Element.CHAPTER: { // part of the start tag + contents WriteSection((Section)element, indent); return; } case Element.LIST: { List list = (List)element; // start tag AddTabs(indent); if (list.IsNumbered()) { WriteStart(HtmlTags.ORDEREDLIST); } else { WriteStart(HtmlTags.UNORDEREDLIST); } if (HasMarkupAttributes(list)) { WriteMarkupAttributes(list); } os.WriteByte(GT); // contents foreach (IElement i in list.Items) { Write(i, indent + 1); } // end tag AddTabs(indent); if (list.IsNumbered()) { WriteEnd(HtmlTags.ORDEREDLIST); } else { WriteEnd(HtmlTags.UNORDEREDLIST); } return; } case Element.LISTITEM: { ListItem listItem = (ListItem)element; styleAttributes = new Properties(); if (listItem.LeadingDefined) { styleAttributes[MarkupTags.CSS_KEY_LINEHEIGHT] = listItem.Leading.ToString() + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.LISTITEM); if (HasMarkupAttributes(listItem)) { WriteMarkupAttributes(listItem); } Write(listItem.Font, styleAttributes); os.WriteByte(GT); currentfont.Push(listItem.Font); // contents foreach (IElement i in listItem) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.LISTITEM); currentfont.Pop(); return; } case Element.CELL: { Cell cell = (Cell)element; // start tag AddTabs(indent); if (cell.Header) { WriteStart(HtmlTags.HEADERCELL); } else { WriteStart(HtmlTags.CELL); } if (HasMarkupAttributes(cell)) { WriteMarkupAttributes(cell); } if (cell.BorderWidth != Rectangle.UNDEFINED) { Write(HtmlTags.BORDERWIDTH, cell.BorderWidth.ToString()); } if (cell.BorderColor != null) { Write(HtmlTags.BORDERCOLOR, HtmlEncoder.Encode(cell.BorderColor)); } if (cell.BackgroundColor != null) { Write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.Encode(cell.BackgroundColor)); } String alignment = HtmlEncoder.GetAlignment(cell.HorizontalAlignment); if (!"".Equals(alignment)) { Write(HtmlTags.HORIZONTALALIGN, alignment); } alignment = HtmlEncoder.GetAlignment(cell.VerticalAlignment); if (!"".Equals(alignment)) { Write(HtmlTags.VERTICALALIGN, alignment); } if (cell.CellWidth != null) { Write(HtmlTags.WIDTH, cell.CellWidth); } if (cell.Colspan != 1) { Write(HtmlTags.COLSPAN, cell.Colspan.ToString()); } if (cell.Rowspan != 1) { Write(HtmlTags.ROWSPAN, cell.Rowspan.ToString()); } if (cell.NoWrap) { Write(HtmlTags.NOWRAP, "true"); } os.WriteByte(GT); // contents if (cell.IsEmpty()) { Write(NBSP); } else { foreach (IElement i in cell.Elements) { Write(i, indent + 1); } } // end tag AddTabs(indent); if (cell.Header) { WriteEnd(HtmlTags.HEADERCELL); } else { WriteEnd(HtmlTags.CELL); } return; } case Element.ROW: { Row row = (Row)element; // start tag AddTabs(indent); WriteStart(HtmlTags.ROW); if (HasMarkupAttributes(row)) { WriteMarkupAttributes(row); } os.WriteByte(GT); // contents IElement cell; for (int i = 0; i < row.Columns; i++) { if ((cell = (IElement)row.GetCell(i)) != null) { Write(cell, indent + 1); } } // end tag AddTabs(indent); WriteEnd(HtmlTags.ROW); return; } case Element.TABLE: { Table table; try { table = (Table)element; } catch (InvalidCastException) { table = ((SimpleTable)element).CreateTable(); } table.Complete(); // start tag AddTabs(indent); WriteStart(HtmlTags.TABLE); if (HasMarkupAttributes(table)) { WriteMarkupAttributes(table); } os.WriteByte(SPACE); Write(HtmlTags.WIDTH); os.WriteByte(EQUALS); os.WriteByte(QUOTE); if (!"".Equals(table.AbsWidth)) { Write(table.AbsWidth); } else { Write(table.WidthPercentage.ToString()); Write("%"); } os.WriteByte(QUOTE); String alignment = HtmlEncoder.GetAlignment(table.Alignment); if (!"".Equals(alignment)) { Write(HtmlTags.ALIGN, alignment); } Write(HtmlTags.CELLPADDING, table.Cellpadding.ToString()); Write(HtmlTags.CELLSPACING, table.Cellspacing.ToString()); if (table.BorderWidth != Rectangle.UNDEFINED) { Write(HtmlTags.BORDERWIDTH, table.BorderWidth.ToString()); } if (table.BorderColor != null) { Write(HtmlTags.BORDERCOLOR, HtmlEncoder.Encode(table.BorderColor)); } if (table.BackgroundColor != null) { Write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.Encode(table.BackgroundColor)); } os.WriteByte(GT); // contents foreach (Row row in table) { Write(row, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.TABLE); return; } case Element.ANNOTATION: { Annotation annotation = (Annotation)element; WriteComment(annotation.Title + ": " + annotation.Content); if (HasMarkupAttributes(annotation)) { os.Write(BEGINCOMMENT, 0, BEGINCOMMENT.Length); WriteMarkupAttributes(annotation); os.Write(ENDCOMMENT, 0, ENDCOMMENT.Length); } return; } case Element.IMGRAW: case Element.JPEG: case Element.IMGTEMPLATE: { Image image = (Image)element; if (image.Url == null) { return; } // start tag AddTabs(indent); WriteStart(HtmlTags.IMAGE); String path = image.Url.ToString(); if (imagepath != null) { if (path.IndexOf("/") > 0) { path = imagepath + path.Substring(path.LastIndexOf("/") + 1); } else { path = imagepath + path; } } Write(HtmlTags.URL, path); if ((image.Alignment & Image.RIGHT_ALIGN) > 0) { Write(HtmlTags.ALIGN, HtmlTags.ALIGN_RIGHT); } else if ((image.Alignment & Image.MIDDLE_ALIGN) > 0) { Write(HtmlTags.ALIGN, HtmlTags.ALIGN_MIDDLE); } else { Write(HtmlTags.ALIGN, HtmlTags.ALIGN_LEFT); } if (image.Alt != null) { Write(HtmlTags.ALT, image.Alt); } Write(HtmlTags.PLAINWIDTH, image.ScaledWidth.ToString()); Write(HtmlTags.PLAINHEIGHT, image.ScaledHeight.ToString()); if (HasMarkupAttributes(image)) { WriteMarkupAttributes(image); } WriteEnd(); return; } default: return; } }
/// <summary> /// Writes a Metatag in the header. /// @throws IOException /// </summary> protected void Write(IElement element, int indent) { Properties styleAttributes = null; switch (element.Type) { case Element.MARKED: { try { Add(element); } catch (DocumentException) { } return; } case Element.CHUNK: { var chunk = (Chunk)element; // if the chunk contains an image, return the image representation var image = chunk.GetImage(); if (image != null) { Write(image, indent); return; } if (chunk.IsEmpty()) { return; } var attributes = chunk.Attributes; if (attributes != null && attributes[Chunk.NEWPAGE] != null) { return; } var tag = IsOtherFont(chunk.Font) || Markup.Count > 0; if (tag) { // start span tag AddTabs(indent); WriteStart(HtmlTags.SPAN); if (IsOtherFont(chunk.Font)) { Write(chunk.Font, null); } WriteMarkupAttributes(Markup); Os.WriteByte(GT); } if (attributes != null && attributes[Chunk.SUBSUPSCRIPT] != null) { // start sup or sub tag if ((float)attributes[Chunk.SUBSUPSCRIPT] > 0) { WriteStart(HtmlTags.SUP); } else { WriteStart(HtmlTags.SUB); } Os.WriteByte(GT); } // contents Write(HtmlEncoder.Encode(chunk.Content)); if (attributes != null && attributes[Chunk.SUBSUPSCRIPT] != null) { // end sup or sub tag Os.WriteByte(LT); Os.WriteByte(FORWARD); if ((float)attributes[Chunk.SUBSUPSCRIPT] > 0) { Write(HtmlTags.SUP); } else { Write(HtmlTags.SUB); } Os.WriteByte(GT); } if (tag) { // end tag WriteEnd(html.Markup.HTML_TAG_SPAN); } return; } case Element.PHRASE: { var phrase = (Phrase)element; styleAttributes = new Properties(); if (phrase.HasLeading()) { styleAttributes[html.Markup.CSS_KEY_LINEHEIGHT] = phrase.Leading + "pt"; } // start tag AddTabs(indent); WriteStart(html.Markup.HTML_TAG_SPAN); WriteMarkupAttributes(Markup); Write(phrase.Font, styleAttributes); Os.WriteByte(GT); Currentfont.Push(phrase.Font); // contents foreach (IElement i in phrase) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(html.Markup.HTML_TAG_SPAN); Currentfont.Pop(); return; } case Element.ANCHOR: { var anchor = (Anchor)element; styleAttributes = new Properties(); if (anchor.HasLeading()) { styleAttributes[html.Markup.CSS_KEY_LINEHEIGHT] = anchor.Leading + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.ANCHOR); if (anchor.Name != null) { Write(HtmlTags.NAME, anchor.Name); } if (anchor.Reference != null) { Write(HtmlTags.REFERENCE, anchor.Reference); } WriteMarkupAttributes(Markup); Write(anchor.Font, styleAttributes); Os.WriteByte(GT); Currentfont.Push(anchor.Font); // contents foreach (IElement i in anchor) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.ANCHOR); Currentfont.Pop(); return; } case Element.PARAGRAPH: { var paragraph = (Paragraph)element; styleAttributes = new Properties(); if (paragraph.HasLeading()) { styleAttributes[html.Markup.CSS_KEY_LINEHEIGHT] = paragraph.TotalLeading + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.DIV); WriteMarkupAttributes(Markup); var alignment = HtmlEncoder.GetAlignment(paragraph.Alignment); if (!"".Equals(alignment)) { Write(HtmlTags.ALIGN, alignment); } Write(paragraph.Font, styleAttributes); Os.WriteByte(GT); Currentfont.Push(paragraph.Font); // contents foreach (IElement i in paragraph) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.DIV); Currentfont.Pop(); return; } case Element.SECTION: case Element.CHAPTER: { // part of the start tag + contents WriteSection((Section)element, indent); return; } case Element.LIST: { var list = (List)element; // start tag AddTabs(indent); if (list.Numbered) { WriteStart(HtmlTags.ORDEREDLIST); } else { WriteStart(HtmlTags.UNORDEREDLIST); } WriteMarkupAttributes(Markup); Os.WriteByte(GT); // contents foreach (IElement i in list.Items) { Write(i, indent + 1); } // end tag AddTabs(indent); if (list.Numbered) { WriteEnd(HtmlTags.ORDEREDLIST); } else { WriteEnd(HtmlTags.UNORDEREDLIST); } return; } case Element.LISTITEM: { var listItem = (ListItem)element; styleAttributes = new Properties(); if (listItem.HasLeading()) { styleAttributes[html.Markup.CSS_KEY_LINEHEIGHT] = listItem.TotalLeading + "pt"; } // start tag AddTabs(indent); WriteStart(HtmlTags.LISTITEM); WriteMarkupAttributes(Markup); Write(listItem.Font, styleAttributes); Os.WriteByte(GT); Currentfont.Push(listItem.Font); // contents foreach (IElement i in listItem) { Write(i, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.LISTITEM); Currentfont.Pop(); return; } case Element.CELL: { var cell = (Cell)element; // start tag AddTabs(indent); if (cell.Header) { WriteStart(HtmlTags.HEADERCELL); } else { WriteStart(HtmlTags.CELL); } WriteMarkupAttributes(Markup); if (cell.BorderWidth.ApproxNotEqual(Rectangle.UNDEFINED)) { Write(HtmlTags.BORDERWIDTH, cell.BorderWidth.ToString(CultureInfo.InvariantCulture)); } if (cell.BorderColor != null) { Write(HtmlTags.BORDERCOLOR, HtmlEncoder.Encode(cell.BorderColor)); } if (cell.BackgroundColor != null) { Write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.Encode(cell.BackgroundColor)); } var alignment = HtmlEncoder.GetAlignment(cell.HorizontalAlignment); if (!"".Equals(alignment)) { Write(HtmlTags.HORIZONTALALIGN, alignment); } alignment = HtmlEncoder.GetAlignment(cell.VerticalAlignment); if (!"".Equals(alignment)) { Write(HtmlTags.VERTICALALIGN, alignment); } if (cell.GetWidthAsString() != null) { Write(HtmlTags.WIDTH, cell.GetWidthAsString()); } if (cell.Colspan != 1) { Write(HtmlTags.COLSPAN, cell.Colspan.ToString()); } if (cell.Rowspan != 1) { Write(HtmlTags.ROWSPAN, cell.Rowspan.ToString()); } if (cell.MaxLines == 1) { Write(HtmlTags.STYLE, "white-space: nowrap;"); } Os.WriteByte(GT); // contents if (cell.IsEmpty()) { Write(NBSP); } else { foreach (IElement i in cell.Elements) { Write(i, indent + 1); } } // end tag AddTabs(indent); if (cell.Header) { WriteEnd(HtmlTags.HEADERCELL); } else { WriteEnd(HtmlTags.CELL); } return; } case Element.ROW: { var row = (Row)element; // start tag AddTabs(indent); WriteStart(HtmlTags.ROW); WriteMarkupAttributes(Markup); Os.WriteByte(GT); // contents IElement cell; for (var i = 0; i < row.Columns; i++) { if ((cell = (IElement)row.GetCell(i)) != null) { Write(cell, indent + 1); } } // end tag AddTabs(indent); WriteEnd(HtmlTags.ROW); return; } case Element.TABLE: { Table table; try { table = (Table)element; } catch (InvalidCastException) { table = ((SimpleTable)element).CreateTable(); } table.Complete(); // start tag AddTabs(indent); WriteStart(HtmlTags.TABLE); WriteMarkupAttributes(Markup); Os.WriteByte(SPACE); Write(HtmlTags.WIDTH); Os.WriteByte(EQUALS); Os.WriteByte(QUOTE); Write(table.Width.ToString(CultureInfo.InvariantCulture)); if (!table.Locked) { Write("%"); } Os.WriteByte(QUOTE); var alignment = HtmlEncoder.GetAlignment(table.Alignment); if (!"".Equals(alignment)) { Write(HtmlTags.ALIGN, alignment); } Write(HtmlTags.CELLPADDING, table.Cellpadding.ToString(CultureInfo.InvariantCulture)); Write(HtmlTags.CELLSPACING, table.Cellspacing.ToString(CultureInfo.InvariantCulture)); if (table.BorderWidth.ApproxNotEqual(Rectangle.UNDEFINED)) { Write(HtmlTags.BORDERWIDTH, table.BorderWidth.ToString(CultureInfo.InvariantCulture)); } if (table.BorderColor != null) { Write(HtmlTags.BORDERCOLOR, HtmlEncoder.Encode(table.BorderColor)); } if (table.BackgroundColor != null) { Write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.Encode(table.BackgroundColor)); } Os.WriteByte(GT); // contents foreach (Row row in table) { Write(row, indent + 1); } // end tag AddTabs(indent); WriteEnd(HtmlTags.TABLE); return; } case Element.ANNOTATION: { var annotation = (Annotation)element; WriteComment(annotation.Title + ": " + annotation.Content); return; } case Element.IMGRAW: case Element.JPEG: case Element.JPEG2000: case Element.IMGTEMPLATE: { var image = (Image)element; if (image.Url == null) { return; } // start tag AddTabs(indent); WriteStart(HtmlTags.IMAGE); var path = image.Url.ToString(); if (Imagepath != null) { if (path.IndexOf("/", StringComparison.Ordinal) > 0) { path = Imagepath + path.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1); } else { path = Imagepath + path; } } Write(HtmlTags.URL, path); if ((image.Alignment & Image.RIGHT_ALIGN) > 0) { Write(HtmlTags.ALIGN, HtmlTags.ALIGN_RIGHT); } else if ((image.Alignment & Image.MIDDLE_ALIGN) > 0) { Write(HtmlTags.ALIGN, HtmlTags.ALIGN_MIDDLE); } else { Write(HtmlTags.ALIGN, HtmlTags.ALIGN_LEFT); } if (image.Alt != null) { Write(HtmlTags.ALT, image.Alt); } Write(HtmlTags.PLAINWIDTH, image.ScaledWidth.ToString(CultureInfo.InvariantCulture)); Write(HtmlTags.PLAINHEIGHT, image.ScaledHeight.ToString(CultureInfo.InvariantCulture)); WriteMarkupAttributes(Markup); WriteEnd(); return; } default: return; } }