private static void WriteDataToPdf(PdfGenerator pdfGenerator, string title, byte[] imageResult, TableData tableData) { pdfGenerator.AddHeader(string.Format("Визуальный словарь на тему «{0}»", title), false); pdfGenerator.AddImage(imageResult, true); pdfGenerator.NewPage(); pdfGenerator.AddTable(tableData, false); }
public byte[] Generate(LanguageShortName from, LanguageShortName to, HashSet <long> visualDictionariesIds) { var languagesQuery = new LanguagesQuery(from, to); UserLanguages userLanguages = languagesQuery.GetLanguagesByShortNames(from, to); var representationsQuery = new RepresentationsQuery(userLanguages.From.Id); List <RepresentationForUser> allRepresentations = representationsQuery.GetBought(userLanguages, visualDictionariesIds); PdfGenerator commonPdfGenerator = allRepresentations.Count > 1 ? new PdfGenerator(_fontPath, _domain, "Все визуальные словари") : null; var zipKey = GetZipKey(allRepresentations, @from, to); var result = _cache.Get(zipKey); if (result != null) { return(result); } var painter = new Painter(); var partImageCreator = new PartImageCreator(painter); foreach (RepresentationForUser representation in allRepresentations) { var tableData = new TableData(3, true); tableData.AddHeader(string.Empty, "Слово", "Перевод"); string title = representation.Title; /*if (pictureName != "Лицо" /*&& pictureName != "Человек"#1#) { * continue; * }*/ var byteStream = new MemoryStream(representation.Image); Image image = Image.FromStream(byteStream); var wordsWriter = new WordsWriter(painter, image.Width, image.Height); foreach (RepresentationAreaForUser area in representation.Areas) { Point leftCorner = area.LeftUpperCorner; Point rightCorner = area.RightBottomCorner; string partKey = representation.Id + "_part_" + area.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + ".jpeg"; byte[] partImageBytes = _cache.Get(partKey); if (partImageBytes == null) { PartImageData partImageData = partImageCreator.CutImage(leftCorner, rightCorner, image); partImageBytes = ImageToBytes(partImageData.Bitmap); WriteToCache(partKey, partImageBytes); } tableData.AddRow(new[] { TableDataCell.CreateImage(partImageBytes), TableDataCell.CreateText(area.Source.Text), TableDataCell.CreateText(area.Translation.Text) }); wordsWriter.AddRectangle(leftCorner, rightCorner, area); } string fullKey = representation.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + ".jpeg"; byte[] imageResult = _cache.Get(fullKey); if (imageResult == null) { Image imageWithSign = wordsWriter.GetImageWithSigns(image); imageResult = ImageToBytes(imageWithSign); WriteToCache(fullKey, imageResult); } var pdfFileName = title + ".pdf"; string pdfKey = representation.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + "_" + pdfFileName; byte[] pdfContent = _cache.Get(pdfKey); if (pdfContent == null) { var pdfGenerator = new PdfGenerator(_fontPath, _domain, string.Format( "Визуальный словарь на тему {0}", title)); WriteDataToPdf(pdfGenerator, title, imageResult, tableData); pdfContent = pdfGenerator.GetAsBytes(); WriteToCache(pdfKey, pdfContent); } _zipCompressor.AddFileToArchive(pdfFileName, pdfContent); if (commonPdfGenerator != null) { WriteDataToPdf(commonPdfGenerator, title, imageResult, tableData); commonPdfGenerator.NewPage(); } } if (commonPdfGenerator != null) { WritePdfToArchive(commonPdfGenerator, "Всё в одном файле.pdf"); } result = _zipCompressor.GetArchive(); WriteToCache(zipKey, result); return(result); }