private void GenerateChapterHeader(string name, string chapterNumber, string alternateTitle, bool pageBreak, Document document) { var chapters = chapterNumber.Split('.'); var level = chapters.Count() - 1; var title = string.IsNullOrWhiteSpace(alternateTitle) ? name.ToUpper() : alternateTitle.ToUpper(); var link = EasyUtilities.SlugifyTitle(title); var chunk = new Chunk(title, _headerFonts[level]); chunk.SetLocalDestination(link); var header = new Paragraph(chunk) { SpacingBefore = _headerFonts[level].Size * 0.8f, SpacingAfter = _headerFonts[level].Size * 0.8f }; if (level == 0) { if (_parent != null && _hierarchy.Any()) { document.Add(_hierarchy.Last()); } _hierarchy.Clear(); var empty = _pdfWriter.PageEmpty; var oddNumber = _pdfWriter.CurrentPageNumber % 2 == 1; if (!empty && oddNumber) { document.Add(Chunk.NEXTPAGE); _pdfWriter.PageEmpty = false; } else if (empty && !oddNumber) { _pdfWriter.PageEmpty = false; } _parent = new iTextSharp.text.Chapter(header, int.Parse(chapters[0])); } else { if (pageBreak) { document.Add(Chunk.NEXTPAGE); } if (level > _hierarchy.Count) { _hierarchy.Push(_parent); } else { while (_hierarchy.Count > level) { _parent = _hierarchy.Pop(); } _parent = _hierarchy.Peek(); } _parent = _parent.AddSection(header); _parent.NumberStyle = Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT; } }
public IElement GetContent() { var paragraph = new Paragraph(); var table = new PdfPTable(1) { WidthPercentage = 100 }; table.DefaultCell.Border = 0; table.DefaultCell.Padding = 2.0f; table.DefaultCell.FixedHeight = 25; table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(new Phrase("BRUKSANVISNING", PdfFonts.Header1)); table.AddCell(new Phrase(_doc.Name, PdfFonts.Header2)); if (_doc.Image != null) { var src = _doc.Image.GetPath(); if (!EasyUtilities.IsAbsoluteUrl(src)) { src = _root + "/" + src; } try { var image = Image.GetInstance(src); image.ScaleToFit(Math.Min(460, image.Width), Math.Min(575, image.Height)); var cell = new PdfPCell(image) { Border = 0, FixedHeight = 575, Padding = 0, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE }; table.AddCell(cell); } catch { table.AddCell(new Phrase("BILDET " + src + " IKKE FUNNET!")); } } else { var cell = new PdfPCell { Border = 0, FixedHeight = 500 }; table.AddCell(cell); } table.DefaultCell.FixedHeight = 16; table.AddCell(new Phrase(_doc.Project.ProjectNumber, PdfFonts.Header3)); if (_doc.Project.Customer != null) { table.AddCell(new Phrase(_doc.Project.Customer.Name, PdfFonts.Header3)); } paragraph.Add(table); return(paragraph); }
private void ParseNode(HtmlNode node, DocumentState state) { switch (node.NodeType) { case HtmlNodeType.Document: case HtmlNodeType.Element: _ignoreContent = false; var styles = ParseStyles(GetAttributeValue(node, "style")); _styles.Push(styles); switch (node.Name) { case "a": var url = GetAttributeValue(node, "href"); var anchor = new Anchor { Reference = url }; _contexts.Push(anchor); break; case "br": _contexts.Peek().Add(Chunk.NEWLINE); break; case "p": var paragraph = new Paragraph { SpacingAfter = 8, SpacingBefore = 8 }; ApplyStyles(paragraph, styles); _contexts.Push(paragraph); break; case "img": var src = GetAttributeValue(node, "src"); if (!EasyUtilities.IsAbsoluteUrl(src)) { src = _root + "/" + src; } try { var image = Image.GetInstance(src); ApplyStyles(image, styles); _contexts.Peek().Add(image); } catch { // TODO: Add error list } break; case "strong": state.Font.Bold = true; break; case "em": state.Font.Italic = true; break; case "u": state.Font.Underlined = true; break; case "ul": ++state.ListLevel; var list = new List { IndentationLeft = 10 * state.ListLevel, ListSymbol = new Chunk("• ") }; _contexts.Push(list); _ignoreContent = true; break; case "li": var item = new ListItem { SpacingAfter = 4, SpacingBefore = 4, IndentationRight = 20 }; ApplyStyles(item, styles); _contexts.Push(item); break; case "table": var table = new TableState { BorderWidth = GetAttributeInteger(node, "border"), CellSpacing = GetAttributeInteger(node, "cellspacing"), CellPadding = GetAttributeInteger(node, "cellpadding") }; _tables.Push(table); _ignoreContent = true; break; case "tr": _ignoreContent = true; break; case "th": case "td": _contexts.Push(new PdfElementStore()); break; } foreach (var child in node.ChildNodes) { ParseNode(child, state); } switch (node.Name) { case "a": case "p": case "li": var element = _contexts.Pop(); _contexts.Peek().Add(element); break; case "ul": --state.ListLevel; element = _contexts.Pop(); _contexts.Peek().Add(element); break; case "strong": state.Font.Bold = false; break; case "em": state.Font.Italic = false; break; case "u": state.Font.Underlined = false; break; case "table": var info = _tables.Pop(); var table = new PdfPTable(info.ColumnCount) { SpacingAfter = 8, SpacingBefore = 8, WidthPercentage = 100 }; table.SetWidths(info.ColumnWidths.ToArray()); foreach (var cell in info.Cells) { table.AddCell(cell); } _contexts.Peek().Add(table); break; case "tr": _tables.Peek().ColumnsCounted = true; _ignoreContent = true; break; case "td": case "th": int colspan; int rowspan; var currentTable = _tables.Peek(); if (!int.TryParse(GetAttributeValue(node, "colspan"), out colspan)) { colspan = 1; } if (!int.TryParse(GetAttributeValue(node, "rowspan"), out rowspan)) { rowspan = 1; } if (!currentTable.ColumnsCounted) { currentTable.ColumnCount += colspan; if (styles.ContainsKey("width")) { var value = styles["width"]; value = value.Replace("px", ""); value = value.Replace("%", ""); value = value.Replace("pt", ""); currentTable.ColumnWidths.Add((int)Math.Round(float.Parse(value))); } } var item = _contexts.Pop() as PdfElementStore; PdfPCell newCell; if (item.Elements.Count == 1 && item.Elements[0] is Image) { var e = item.Elements[0]; newCell = new PdfPCell(e as Image, false) { Colspan = colspan, Rowspan = rowspan, BorderWidth = currentTable.BorderWidth, Padding = currentTable.CellPadding, Indent = currentTable.CellSpacing, VerticalAlignment = Element.ALIGN_MIDDLE }; } else { var paragraph = new Paragraph(); newCell = new PdfPCell(paragraph) { Colspan = colspan, Rowspan = rowspan, BorderWidth = currentTable.BorderWidth, Padding = currentTable.CellPadding, Indent = currentTable.CellSpacing, VerticalAlignment = Element.ALIGN_MIDDLE }; ApplyStyles(paragraph, styles); foreach (var e in item.Elements) { paragraph.Add(e); } newCell.AddElement(paragraph); } ApplyStyles(newCell, styles); currentTable.Cells.Add(newCell); _ignoreContent = true; break; } _styles.Pop(); break; case HtmlNodeType.Text: if (!_ignoreContent) { var text = HtmlEntity.DeEntitize(node.InnerText); text = ReplaceVariables(text, _parameters); if (text.Contains("££")) { var firstPart = text.IndexOf("££", StringComparison.Ordinal); if (firstPart > 0) { var firstPartText = text.Substring(0, firstPart); _contexts.Peek().Add(CreateTextChunk(state, firstPartText)); } text = text.Substring(firstPart + 2); _contexts.Peek().Add(Chunk.NEXTPAGE); } _contexts.Peek().Add(CreateTextChunk(state, text)); } break; } }