private static ArrayList GetParams(byte[] file, int rows, int cols, PdfDocument document) { var list = new ArrayList(); using (var ms = new MemoryStream(file)) { using (var sticker = new PdfDocument(new PdfReader(ms))) { float x = Config.GetInstance().StickerPageSize.Width *(cols - 1); float y = (Config.GetInstance().PrintPageSize.Height - Config.GetInstance().StickerPageSize.Height) - (Config.GetInstance().StickerPageSize.Height *rows); PdfFormXObject pdfFormXObject = sticker.GetFirstPage().CopyAsFormXObject(document); list.Add(pdfFormXObject); list.Add(x); list.Add(y); } } return(list); }
private static void CheckedFileToExist(ref FileInfo info) { if (info.Exists) { Log.Information("File for saving is exist!"); switch (Config.GetInstance().FileExistRule) { case FileExistRuleType.Replace: Log.Information("Replace old file: FILE[ {@file} ]", info.FullName); info.Delete(); break; case FileExistRuleType.Rename: // Todo: create rename algoritm // send message to UI and showing dialog window to user, // where we ask new name for file, then return this name // and replace him in path string break; case FileExistRuleType.RandomName: var name = Path.GetFileNameWithoutExtension(info.Name); var extantion = Path.GetExtension(info.Name); // add random name to name name += "_" + Path.GetRandomFileName(); // get new FileInfo string newFile = Path.Combine(info.DirectoryName ?? throw new InvalidOperationException(), name + extantion); info = new FileInfo(newFile); Log.Information("Generate random prefix to file: FILE[ {@file} ]", newFile); break; case FileExistRuleType.None: default: throw new ArgumentOutOfRangeException(); } } }
public static void WriteToFileWithNup(IEnumerable <Sticker> stickers, Queue <Page> pages, string file) { // check path if (string.IsNullOrEmpty(file) || string.IsNullOrWhiteSpace(file)) { Log.Fatal("File for saving can not be empty or nullable! \nPARAM_NAME[ {@name} ]\nPATH_TO_FILE[ {@file} ]", nameof(file), file); throw new ArgumentNullException(nameof(file)); } var info = new FileInfo(file); // check directory CreateDirectoryIfNotExist(info.Directory); // check file for on exist CheckedFileToExist(ref info); var stickerList = stickers.ToList(); using (var document = new PdfDocument(new PdfWriter(info))) { PrintPage documentPrintPage = Config.GetInstance().PrintPageSize; if (Config.GetInstance().Orientation != documentPrintPage.GetOrientation()) { documentPrintPage = documentPrintPage.Rotation(); } var documentWidth = documentPrintPage.SizeType == SizeType.Mm ? documentPrintPage.GetWidthReverse() : documentPrintPage.Width; var documentHeight = documentPrintPage.SizeType == SizeType.Mm ? documentPrintPage.GetHeightReverse() : documentPrintPage.Height; document.SetDefaultPageSize(new PageSize(documentWidth, documentHeight)); foreach (Page page in pages) { var canvas = new PdfCanvas(document.AddNewPage()); for (int rows = 0; rows < Config.GetInstance().Row; rows++) { for (int cols = 0; cols < Config.GetInstance().Column; cols++) { int index = page.Pages[rows, cols]; if (index == -1) { continue; } using (var ms = new MemoryStream(stickerList[index].File)) { using (var sticker = new PdfDocument(new PdfReader(ms))) { float stickerWidth = Config.GetInstance().StickerPageSize.SizeType == SizeType.Mm ? Config.GetInstance().StickerPageSize.GetWidthReverse() : Config.GetInstance().StickerPageSize.Width; float stickerHeight = Config.GetInstance().StickerPageSize.SizeType == SizeType.Mm ? Config.GetInstance().StickerPageSize.GetHeightReverse() : Config.GetInstance().StickerPageSize.Height; float x = stickerWidth * cols; float y = (documentHeight - stickerHeight) - (stickerHeight * rows); PdfFormXObject pdfFormXObject = sticker.GetFirstPage().CopyAsFormXObject(document); canvas.AddXObject(pdfFormXObject, x, y); } } } } } } }