public static void CreateDocument(FinalDocument model) { model.Document = new Document(); var document = model.Document; document.Info.Title = model.Title; document.Info.Subject = model.Title; document.Info.Author = string.IsNullOrEmpty(model.Author) ? "Automated generate report" : model.Author; }
public static void DefineTableOfContents(FinalDocument model) { if (!model.TOC) { return; } Section section = model.Document.AddSection(); section.PageSetup.LeftMargin = 40; section.PageSetup.RightMargin = 40; section.AddPageBreak(); Paragraph paragraph = section.AddParagraph("Contents"); paragraph.Format.Font.Size = 20; paragraph.Format.Font.Bold = true; paragraph.Format.SpaceAfter = 24; paragraph.Format.OutlineLevel = OutlineLevel.Level1; paragraph.Format.SpaceAfter = "2cm"; // Main title model.MainTitle.ToList().ForEach(x => { paragraph = section.AddParagraph(); paragraph.Style = "TOC 1"; Hyperlink hyperlinkLvl1 = null; // Sub title x.SubTitle.ToList().ForEach(u => { if (hyperlinkLvl1 == null) { hyperlinkLvl1 = paragraph.AddHyperlink(u.Id); hyperlinkLvl1.AddText($"{x.Title}\t"); hyperlinkLvl1.AddPageRefField(u.Id); paragraph.Format.SpaceAfter = "0.25cm"; } paragraph = section.AddParagraph(); paragraph.Style = "TOC 2"; Hyperlink hyperlinkLvl2 = paragraph.AddHyperlink(u.Id); hyperlinkLvl2.AddText($"{(char)160}{(char)160}{(char)160}{(char)160}{u.Title}\t"); hyperlinkLvl2.AddPageRefField(u.Id); }); paragraph.Format.SpaceAfter = "1cm"; }); }
public static void DefineCover(FinalDocument model) { if (string.IsNullOrEmpty(model.Title) && string.IsNullOrEmpty(model.Logo)) { return; } Section section = model.Document.AddSection(); Paragraph paragraph; paragraph = section.AddParagraph(model.Title); paragraph.Format.Font.Size = 32; paragraph.Format.Font.Color = Colors.Black; paragraph.Format.Font.Bold = true; paragraph.Format.Alignment = ParagraphAlignment.Center; paragraph.Format.SpaceBefore = "10cm"; paragraph.Format.SpaceAfter = "12cm"; }
public static void MergeFiles(FinalDocument model) { Section section = model.Document.LastSection; // For each title model.MainTitle.ToList().ForEach(mainTitleElm => { bool isTitleAdded = false; // For each subtitle mainTitleElm.SubTitle.ToList().ForEach(subtitleElm => { bool isSubTitleAdded = false; PdfDocument inputDocument = PdfReader.Open(subtitleElm.FilePath, PdfDocumentOpenMode.Import); // Iterate pages int count = inputDocument.PageCount; for (int idx = 0; idx < count; idx++) { section = model.Document.AddSection(); #region Define the orientation bool isLandscape = inputDocument.Pages[idx].Orientation == PdfSharpCore.PageOrientation.Landscape; section.PageSetup.PageHeight = isLandscape ? inputDocument.Pages[idx].Width.Value : inputDocument.Pages[idx].Height.Value; section.PageSetup.PageWidth = isLandscape ? inputDocument.Pages[idx].Height.Value : inputDocument.Pages[idx].Width.Value; section.PageSetup.Orientation = isLandscape ? Orientation.Landscape : Orientation.Portrait; #endregion isTitleAdded = AddTitle(isTitleAdded, section, mainTitleElm, subtitleElm); isSubTitleAdded = AddSubtitle(isSubTitleAdded, section, subtitleElm); #region Add the page Image image = section.AddImage($"{subtitleElm.FilePath}#{idx + 1}"); image.LockAspectRatio = true; image.RelativeHorizontal = RelativeHorizontal.Page; image.RelativeVertical = RelativeVertical.Page; image.WrapFormat.Style = WrapStyle.Through; #endregion } }); }); }
public static void Generate(EntryParameters parameters) { try { DisplayArguments(parameters); var model = new FinalDocument(parameters); // Create the document Documents.CreateDocument(model); // Define the styles DocumentStyles.DefineStyles(model); // Create the cover page Cover.DefineCover(model); // Create the TOC TableOfContents.DefineTableOfContents(model); // Merge the files External.MergeFiles(model); // Generate the document GenerateFile(model.Document, parameters); Logger.Log($"Process ended correctly.\n" + $"{model.MainTitle.Select(x => x.SubTitle.Count()).Sum()} files were merged.\n" + $"You can find everything inside => {parameters.WorkPath}"); } catch (Exception ex) { Logger.Error(ex.Message); } finally { Logger.Log("Process finished...\n"); } }
private string GenerateHtml(IEnumerable <ContentType> contentTypes, IEnumerable <Module> modules) { var contentTypeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views", "ContentType.cshtml"); var moduleFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views", "Module.cshtml"); var finalDocumentFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views", "FinalDocument.cshtml"); var templateService = new TemplateService(); templateService.AddNamespace("uSyncScrapper.Models"); var contentTypeBody = new StringBuilder(); var modulesBody = new StringBuilder(); foreach (var contentType in contentTypes) { contentTypeBody.Append(templateService.Parse(File.ReadAllText(contentTypeFilePath), contentType, null, "ContentType")); } foreach (var module in modules) { modulesBody.Append(templateService.Parse(File.ReadAllText(moduleFilePath), module, null, "Module")); } var finalDocType = new FinalDocument { DocTypesBody = contentTypeBody.ToString(), DocTypes = contentTypes, ModulesBody = modulesBody.ToString(), Modules = modules }; var finalDocument = templateService.Parse(File.ReadAllText(finalDocumentFilePath), finalDocType, null, "FinalDocument"); return(WebUtility.HtmlDecode(finalDocument)); }
/// <summary> /// Defines the styles used in the document. /// </summary> public static void DefineStyles(FinalDocument model) { var document = model.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 1", "Normal"); style.ParagraphFormat.AddTabStop("18cm", TabAlignment.Right, TabLeader.Dots); style.ParagraphFormat.Font.Color = Colors.Black; style.ParagraphFormat.Font.Size = 16; // Create a new style called TOC based on style Normal style = document.Styles.AddStyle("TOC 2", "Normal"); style.ParagraphFormat.AddTabStop("18cm", TabAlignment.Right, TabLeader.Dots); style.ParagraphFormat.Font.Size = 14; }