// // Class: Document // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 5/27/2015 8:52:50 PM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Collections.Generic; using System.IO; using MigraDoc; using MigraDoc.DocumentObjectModel; using MigraDoc.DocumentObjectModel.Shapes; using MigraDoc.DocumentObjectModel.Tables; using MigraDoc.Rendering; using bsmd.database; namespace bsmd.ReportGenerator { public class BSMDDocument { #region create document public static Document CreateDocument(string title, string subject, string author) { // Create a new MigraDoc document Document document = new Document(); if(title != null) document.Info.Title = title; if(subject != null) document.Info.Subject = subject; if(author != null) document.Info.Author = author; BSMDDocument.DefineStyles(document); BSMDDocument.DefineCover(document); BSMDDocument.DefineContentSection(document); //TableOfContents.DefineTableOfContents(document); //DefineContentSection(document); //Paragraphs.DefineParagraphs(document); //Tables.DefineTables(document); //Charts.DefineCharts(document); return document; } /// /// create the final output document /// /// document to render /// full path of bla.pdf output file public static void RenderDocument(Document document, string filename) { // wozu braucht man das hier? string filenameCore = Path.GetFileNameWithoutExtension(filename); string migraTempFile = string.Format("{1}\\{0}.mdddl", filenameCore, Path.GetDirectoryName(filename)); MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, migraTempFile); // TODO Test font embedding (-> filesize!) PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.None); renderer.Document = document; renderer.RenderDocument(); renderer.PdfDocument.Save(filename); } #endregion #region Style public static void DefineStyles(Document document) { // Get the predefined style Normal. Style style = document.Styles["Normal"]; // Because all styles are derived from Normal, the next line changes the // font of the whole document. Or, more exactly, it changes the font of // all styles and paragraphs that do not redefine the font. style.Font.Name = "Times New Roman"; // Heading1 to Heading9 are predefined styles with an outline level. An outline level // other than OutlineLevel.BodyText automatically creates the outline (or bookmarks) // in PDF. style = document.Styles["Heading1"]; style.Font.Name = "Tahoma"; style.Font.Size = 14; style.Font.Bold = true; style.Font.Color = Colors.DarkBlue; style.ParagraphFormat.PageBreakBefore = true; style.ParagraphFormat.SpaceAfter = 6; style = document.Styles["Heading2"]; style.Font.Size = 12; style.Font.Bold = true; style.ParagraphFormat.PageBreakBefore = false; style.ParagraphFormat.SpaceBefore = 6; style.ParagraphFormat.SpaceAfter = 6; style = document.Styles["Heading3"]; style.Font.Size = 10; style.Font.Bold = true; style.Font.Italic = true; style.ParagraphFormat.SpaceBefore = 6; style.ParagraphFormat.SpaceAfter = 3; style = document.Styles[StyleNames.Header]; style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right); style = document.Styles[StyleNames.Footer]; style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center); // Create a new style called TextBox based on style Normal style = document.Styles.AddStyle("TextBox", "Normal"); style.ParagraphFormat.Alignment = ParagraphAlignment.Justify; style.ParagraphFormat.Borders.Width = 2.5; style.ParagraphFormat.Borders.Distance = "3pt"; style.ParagraphFormat.Shading.Color = Colors.SkyBlue; // Create a new style called TOC based on style Normal style = document.Styles.AddStyle("TOC", "Normal"); style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots); style.ParagraphFormat.Font.Color = Colors.Blue; } #endregion #region Cover /// /// Defines the cover page. /// public static void DefineCover(Document document) { Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); paragraph.Format.SpaceAfter = "3cm"; Image image = section.AddImage(Properties.Settings.Default.LogoPath); image.Width = "3cm"; paragraph = section.AddParagraph("EU-NOAD incoming data receipt"); paragraph.Format.Font.Size = 16; paragraph.Format.Font.Color = Colors.DarkRed; paragraph.Format.SpaceBefore = "8cm"; paragraph.Format.SpaceAfter = "3cm"; paragraph = section.AddParagraph("Rendering date: "); paragraph.AddDateField(); } #endregion #region setup, header and footers /// /// Defines page setup, headers, and footers. /// public static void DefineContentSection(Document document) { Section section = document.AddSection(); section.PageSetup.OddAndEvenPagesHeaderFooter = true; section.PageSetup.StartingNumber = 1; HeaderFooter header = section.Headers.Primary; header.AddParagraph("\tEU-NOAD receive receipt"); header = section.Headers.EvenPage; header.AddParagraph("EU-NOAD receive receipt"); // Create a paragraph with centered page number. See definition of style "Footer". Paragraph paragraph = new Paragraph(); paragraph.AddTab(); paragraph.AddPageField(); // Add paragraph to footer for odd pages. section.Footers.Primary.Add(paragraph); // Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must // not belong to more than one other object. If you forget cloning an exception is thrown. section.Footers.EvenPage.Add(paragraph.Clone()); } #endregion #region NSW data public static void AddNSWMessageParagraph(Document document, IMessageParagraph messageParagraph) { Paragraph paragraph = document.LastSection.AddParagraph(messageParagraph.Title, "Heading2"); if (messageParagraph.MessageText != null) // 1:n message (CREW, PAS,..) BSMDDocument.AddActualTableParagraph(document, messageParagraph.MessageText, false); if (messageParagraph.ChildParagraphs != null) { foreach (IMessageParagraph childParagraph in messageParagraph.ChildParagraphs) { BSMDDocument.AddActualTableParagraph(document, childParagraph.MessageText, true); } } } private static void AddActualTableParagraph(Document document, List> messageText, bool isSubTable) { Table table = document.LastSection.AddTable(); // table.LeftPadding = new Unit(0.5, UnitType.Centimeter); Column leadColumn = table.AddColumn(Unit.FromCentimeter(8)); leadColumn.Format.Alignment = ParagraphAlignment.Left; Column mainColumn = table.AddColumn(Unit.FromCentimeter(8)); mainColumn.Format.Alignment = ParagraphAlignment.Left; for (int i = 0; i < messageText.Count; i++) { KeyValuePair elem = messageText[i]; Row row = table.AddRow(); Cell cell = row.Cells[0]; cell.AddParagraph(elem.Key); row.Cells[1].AddParagraph(elem.Value); } if (isSubTable) { table.SetEdge(0, 0, 2, messageText.Count, Edge.Box, BorderStyle.DashLargeGap, new Unit(1.0, UnitType.Point)); } document.LastSection.AddParagraph(); } #endregion } }