public static PdfActionResult CreatePdfPage(PdfActionInsertImage action) { // const int imgPixelFactor = 3; var srcPageIndex = action.SourcePageIndex; var modelPageIndex = action.ModelPageIndex; var resultPageIndex = action.ResultPageIndex; // Calculate Destination rectangle in the form var rectDestination = GetDestinationRect(action.ModelPdfPath, modelPageIndex); // calculate image size var rect = GetPageSize(action.SourcePdfPath, srcPageIndex); var imageSize = new System.Drawing.Size((int)(rect.Width * imgPixelFactor), (int)(rect.Height * imgPixelFactor)); // generate image var imgContentPath = tempDir + Path.GetFileNameWithoutExtension(action.SourcePdfPath) + "-p" + srcPageIndex + ".jpeg"; PdfToJpegConvertor.GetPageAsImage(action.SourcePdfPath, srcPageIndex, imageSize, action.SourceMarginLeft, 0, 0, 0, imgContentPath); if (!File.Exists(imgContentPath)) { throw new Exception($"File do not exsits: '{imgContentPath}'"); } // Insert image into Pdf var resultPdfPathA = tempDir + "page-" + resultPageIndex + "-a.pdf"; InsertImageToPdf(action.ModelPdfPath, resultPdfPathA, imgContentPath, modelPageIndex, rectDestination, action.FullPageLabel); var resultPdfPathB = tempDir + "page-" + resultPageIndex + "-b.pdf"; PdfReplacer.FixPageNumberOnPage(resultPdfPathA, resultPdfPathB, modelPageIndex, resultPageIndex.ToString()); return(new PdfActionResult { Action = action, ResultPdfPath = resultPdfPathB }); }
private static IEnumerable <PdfActionInsertImage> BuildActions(JugementArgs jugementArgs) { var actions = new List <PdfActionInsertImage>(); var rectoPageCount = GetPageCount(jugementArgs.RectoPdfPath); var versoPageCount = GetPageCount(jugementArgs.VersoPdfPath); var totalPageCount = rectoPageCount + versoPageCount; var pageIndexDisplay = 1; // Calculated Ajusted Left Margin double leftMarginInCmAdjusted = 3.9; if (jugementArgs.LeftMarginInCm.HasValue && jugementArgs.LeftMarginInCm >= 0 && jugementArgs.LeftMarginInCm < 21.0) { leftMarginInCmAdjusted = jugementArgs.LeftMarginInCm.Value; // Error correction } leftMarginInCmAdjusted += 0.5; // Error correction int leftMarginInImgPx = (int)Math.Round(leftMarginInCmAdjusted * 1766 / 21); // var pageCountToSkipAtTheEndOfVerso = 2; if (jugementArgs.SkipVersoPages.HasValue && jugementArgs.SkipVersoPages >= 0) { pageCountToSkipAtTheEndOfVerso = jugementArgs.SkipVersoPages.Value; } for (int i = 1; i <= totalPageCount; i++) { var a = new PdfActionInsertImage() { ResultPageIndex = pageIndexDisplay, ModelPdfPath = jugementArgs.FormPdfPath, FullPageLabel = pageIndexDisplay.ToString() // +"/"+ totalPageCount // It's too large ! }; a.SourceMarginLeft = (i == 1) ? 0 : leftMarginInImgPx; // magic number var recto = (i % 2 == 1); if (i == 1) { a.ModelPageIndex = 1; } else { a.ModelPageIndex = 4; } a.SourcePdfPath = recto ? jugementArgs.RectoPdfPath : jugementArgs.VersoPdfPath; var index = (i + 1) / 2; a.SourcePageIndex = recto ? index : versoPageCount + 1 - index; if (!recto && a.SourcePageIndex <= pageCountToSkipAtTheEndOfVerso) { continue; } actions.Add(a); pageIndexDisplay++; } return(actions); }