public long GenerateReport(bool forceReload = false) { /*TIME*/ Stopwatch t = new Stopwatch(); /*TIME*/ t.Start(); Cache.LoadCache(forceReload); /*TIME*/ long t1 = t.ElapsedMilliseconds; IEnumerable <Citation> orderedCitations0 = m_DBService.Select <Citation>(); /*TIME*/ long t11 = t.ElapsedMilliseconds; IEnumerable <Citation> orderedCitations = orderedCitations0 .OrderBy(x => x.VolumeId) .ThenBy(y => y.PhysicalPageStart) .ThenBy(y => y.GlyphStart); /*TIME*/ long t2 = t.ElapsedMilliseconds; int counter = 1; var nullCategory = Cache.Categories[Id.Null]; foreach (Citation c in orderedCitations) { Volume volume = Cache.Volumes[c.VolumeId]; var mainCitCat = Cache.CitationCategories.Where(x => x.CitationId == c.Id)?.SingleOrDefault(x => x.IsMain); var mainCategory = (mainCitCat == null) ? nullCategory : Cache.Categories[mainCitCat.CategoryId]; ReportModel m = new ReportModel { Idx = counter++, VolumeId = c.VolumeId, VolumeTitle = volume?.Title, VolumePublicationDate = volume?.PublicationDate ?? "", CitationId = c.Id, OriginalCitation = c.Citation1, Citation = c.Citation3, Page = VolumeService.GetPageNumberForVolume(volume.Id, Cache.Pages, c.PhysicalPageStart), PhysicalPage = c.PhysicalPageStart, Book = VolumeService.GetReferenceForVolume(volume.Id, Cache.Books, c.PhysicalPageStart, c.GlyphStart)?.Title ?? "", Chapter = VolumeService.GetReferenceForVolume(volume.Id, Cache.Chapters, c.PhysicalPageStart, c.GlyphStart)?.Title ?? "", SubChapter = VolumeService.GetReferenceForVolume(volume.Id, Cache.SubChapters, c.PhysicalPageStart, c.GlyphStart)?.Title ?? "", Paragraph = VolumeService.GetReferenceForVolume(volume.Id, Cache.Paragraphs, c.PhysicalPageStart, c.GlyphStart)?.Title ?? "", MainCategory = mainCategory.ToString(), MainCategoryWeight = mainCitCat?.Weight ?? 0, // Hidden Emphasis = c.Emphasis, }; Report.Add(m); } t.Stop(); /*TIME*/ long tFinal = t.ElapsedMilliseconds; return(tFinal); }
public void RecreateTheWholeThing(ModelsForViewing vm, VolumeService volumeService) { string origFileName = vm.CurrentStorage.FilePath; string storageFilePath = System.IO.Path.Combine(m_UserSettingsService.StorageFolder, vm.CurrentStorage.StorageName); string newFileTmp = m_TempFileService.GetNewTmpFileName(vm.CurrentStorage.StorageName); //File.Copy(storageFilePath, tmpFileName, true); (int x, int y)offset = (volumeService.CurrentVolume.OffsetX, volumeService.CurrentVolume.OffsetY); PdfDocument pdfDoc = new PdfDocument(new PdfReader(origFileName), new PdfWriter(newFileTmp)); foreach (Citation cit in volumeService.Citations) { // Yeah, somtimes illegal citations end up in the database. if (cit.SelectionRects.Trim().Length < 5) { continue; } var cat = m_CategoryService.GetMainCategory(cit.Id); var citCat = m_CategoryService.GetMainCitationCategory(cit.Id); if (cat == null) { citCat.CategoryId = Id.Empty; } List <(int page, int[] rects)> pageRects = ArrayStuff.ConvertStringToPagesAndArrays(cit.SelectionRects); int firstPageNo = GetFirstPageFromPageRects(pageRects); Color color_highLight, color_underLine, color_margin = new DeviceRgb(n(255), n(255), n(255));; if (pageRects.Any()) { System.Drawing.Color ch, cu, cm; var colors = ColorStuff.ConvertStringToColors(cit.CitationColors); ch = (colors.Length >= 1) ? colors[0] : m_UserSettingsService.PdfHighLightColor; cu = (colors.Length >= 2) ? colors[1] : m_UserSettingsService.PdfUnderlineColor; cm = (colors.Length >= 3) ? colors[2] : m_UserSettingsService.PdfMarginBoxColor; color_highLight = new DeviceRgb(n(ch.R), n(ch.G), n(ch.B)); color_underLine = new DeviceRgb(n(cu.R), n(cu.G), n(cu.B)); color_margin = new DeviceRgb(n(cm.R), n(cm.G), n(cm.B)); foreach ((int page, int[] rects)pageRect in pageRects) { if (pageRect.rects.Count() < 4) // If no rects, for some reason... { continue; } //int currentPage = pageRects.First().page; int currentPage = pageRect.page; int currentRect = 0; bool notDone = true; do { PdfCanvas canvas1 = new PdfCanvas( pdfDoc.GetPage(currentPage).NewContentStreamBefore(), pdfDoc.GetPage(currentPage).GetResources(), pdfDoc); PdfCanvas canvas2 = new PdfCanvas( pdfDoc.GetPage(currentPage).NewContentStreamAfter(), pdfDoc.GetPage(currentPage).GetResources(), pdfDoc); Rectangle pageSize = pdfDoc.GetPage(firstPageNo).GetPageSize(); int pageHeight = (int)pageSize.GetHeight(); int pageWidth = (int)pageSize.GetWidth(); //canvas.SaveState(); canvas1.SetFillColor(color_highLight); canvas2.SetFillColor(color_underLine); do //(int i = 0; i < rects.Length; i += 4) { if (currentRect >= pageRect.rects.Length) // Sometime this is necessary, page rects are empty or broken. { break; } int x = pageRect.rects[currentRect] + offset.x; int y = pageHeight - pageRect.rects[currentRect + 1] - pageRect.rects[currentRect + 3] + offset.y; // Below text highlight canvas1.Rectangle(x, y, pageRect.rects[currentRect + 2], pageRect.rects[currentRect + 3]); canvas1.Fill(); // Above text underline canvas2.Rectangle(x, y, pageRect.rects[currentRect + 2], 1); canvas2.Fill(); if (currentRect + 5 > pageRect.rects.Length) { notDone = false; break; } // Detect page break int lastY = pageHeight - pageRect.rects[currentRect + 1] - pageRect.rects[currentRect + 3]; currentRect += 4; int newY = pageHeight - pageRect.rects[currentRect + 1] - pageRect.rects[currentRect + 3]; if (lastY < newY) { currentPage++; break; } } while (true); } while (notDone); } // Add annotation AddMarginBox(pdfDoc, null, firstPageNo, cit.Id.ToString(), cat.Code + $" [{citCat.Weight}]", cit.SelectionRects, color_margin, cit.MarginBoxSettings, offset); }// if pagerects.any } pdfDoc.Close(); // newFileTmp m_StorageHelperService.GetNextStorageFileName(vm.CurrentStorage); string newStoragePath = System.IO.Path.Combine(m_UserSettingsService.StorageFolder, vm.CurrentStorage.StorageName); int retries = 10; do { try { //File.Delete(storageFilePath); File.Copy(newFileTmp, newStoragePath); retries = 0; } catch { retries--; Thread.Sleep(500); } } while (retries > 0); File.Delete(newFileTmp); m_StorageHelperService.DeleteOldStorageFiles(newStoragePath); }