public IDictionary <string, PartialPdfModel> GetPartialPdfModels(IList <string> htmlFilePaths) { Guard.ArgumentNotNull(htmlFilePaths, nameof(htmlFilePaths)); Guard.Argument(() => htmlFilePaths.All(p => !string.IsNullOrEmpty(p)), nameof(htmlFilePaths), $"{nameof(htmlFilePaths)} cannot contain null or empty html file path."); var pdfFileNumberOfPages = new ConcurrentDictionary <string, PartialPdfModel>(); Parallel.ForEach( htmlFilePaths, new ParallelOptions { MaxDegreeOfParallelism = _htmlToPdfOptions.MaxDegreeOfParallelism }, htmlFilePath => { var numberOfPages = Convert($"{WrapQuoteToPath(htmlFilePath)} -", reader => reader.NumberOfPages); PartialPdfModel pdfModel = new PartialPdfModel { FilePath = htmlFilePath, NumberOfPages = numberOfPages }; pdfFileNumberOfPages.TryAdd(htmlFilePath, pdfModel); }); return(pdfFileNumberOfPages); }
private void CreateOutlines(Dictionary <string, object> rootOutline, IList <HtmlModel> htmlModels, IDictionary <string, PartialPdfModel> pdfPages) { if (htmlModels?.Count > 0) { foreach (var htmlModel in htmlModels) { var outline = new Dictionary <string, object> { { "Title", htmlModel.Title }, { OutLineKidsName, new List <Dictionary <string, object> >() } }; if (!string.IsNullOrEmpty(htmlModel.ExternalLink)) { outline.Add("Action", "URI"); outline.Add("URI", htmlModel.ExternalLink); } else { int pageNumber = 0; if (!string.IsNullOrEmpty(htmlModel.HtmlFilePath)) { string filePath = GetFilePath(htmlModel.HtmlFilePath); if (pdfPages.ContainsKey(filePath)) { PartialPdfModel pdfModel = pdfPages[filePath]; if (!pdfModel.PageNumber.HasValue) { pdfModel.PageNumber = _currentNumberOfPages; _currentNumberOfPages += pdfModel.NumberOfPages; } pageNumber = pdfModel.PageNumber.Value; } } else { // this is a parent node for the next topic pageNumber = _currentNumberOfPages; } outline.Add("Action", "GoTo"); // please go to http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfDestination.html to find the detail. outline.Add("Page", $"{pageNumber} FitH"); } ((List <Dictionary <string, object> >)rootOutline[OutLineKidsName]).Add(outline); CreateOutlines(outline, htmlModel.Children, pdfPages); } } }
private void CreateOutlines(PdfOutlineCollection outlineCollection, IList <HtmlModel> htmlModels, IDictionary <string, PartialPdfModel> pdfPages) { if (htmlModels?.Count > 0) { foreach (var htmlModel in htmlModels) { PdfOutline outline = new PdfOutline() { Title = htmlModel.Title, Opened = true }; if (!string.IsNullOrEmpty(htmlModel.ExternalLink)) { outline.Elements.Add("/Type", new PdfString("/Action")); outline.Elements.Add("/Subtype", new PdfString("/Link")); outline.Elements.Add("/A", new PdfLiteral($"<</S/URI/URI({htmlModel.ExternalLink})>>")); } else { if (!string.IsNullOrEmpty(htmlModel.HtmlFilePath)) { string filePath = GetFilePath(htmlModel.HtmlFilePath); if (pdfPages.ContainsKey(filePath)) { PartialPdfModel pdfModel = pdfPages[filePath]; if (!pdfModel.PageNumber.HasValue) { pdfModel.PageNumber = _currentNumberOfPages - 1; _currentNumberOfPages += pdfModel.NumberOfPages; } outline.DestinationPage = outlineCollection.Owner.Pages[pdfModel.PageNumber.Value]; outline.PageDestinationType = PdfPageDestinationType.FitH; } } } outlineCollection.Add(outline); CreateOutlines(outline.Outlines, htmlModel.Children, pdfPages); } } }