最近遇到的問題 : 合併多筆PDF文件,但內容裡面有轉90度跟270度怎麼辦?
第一種,使用pdfReader直接轉, 但如果後續還要進行頁碼等處理則不建議
主要就這句 page.Put(PdfName.ROTATE, new PdfNumber(rotation));
提供rotation 要旋轉的角度
而我有一個參數rotations只是有事先記錄該pdf本身選轉的角度
可以直接使用int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360;
替代即可
public void RotatePDFFiles_bak(string fileList, string outMergeFile, List<int> rotations) { PdfReader reader = new PdfReader(fileList); int pagesCount = reader.NumberOfPages; for (int n = 1; n <= pagesCount; n++) { PdfDictionary page = reader.GetPageN(n); PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE); //int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360; int rotation = 0; if (rotations[n - 1] == 90) { rotation = 180; //(90 + 90) % 360; //向左橫置的pdf逆轉90 } else if (rotations[n - 1] == 270) { rotation = 0; // (270 + 90) % 360; } page.Put(PdfName.ROTATE, new PdfNumber(rotation)); } PdfStamper stamper = new PdfStamper(reader, new FileStream(outMergeFile, FileMode.Create)); stamper.Close(); reader.Close(); }
第二種,如果還有後續操作,我是先把PDF縮到符合A4大小,以及把逆轉90度的橫置pdf頁一併先把page 設定調整轉270度, 所以其實就是轉180度, 從逆轉90度的PDF, 變成順轉90度的pdf
統一所有橫置PDF....
在第二段method, 透過建立新頁面,
writer.DirectContent.AddTemplate(newPage, 1f, 0, 0, 1f, 0, 0);
這方法居然會把橫置pdf統一逆時鐘轉90度, 然後正的pdf還是正的...
所以結果就是我們要的了
我有去研究一下AddTemplate這方法, 但測試出來的參數總是不如預期,只好用這樣分兩次的方式去處理,如果有大大有更正確的方式,歡迎教導一下QQ
public List<int> MergePDFFiles(string[] fileList, string outMergeFile) { List<int> r = new List<int>(); PdfReader reader; Document document = new Document(); //MemoryStream ms = new MemoryStream(); using (var steam = new FileStream(outMergeFile, FileMode.Create)) { PdfWriter writer = PdfWriter.GetInstance(document, steam); //PdfWriter writer = PdfWriter.GetInstance(document, ms); //var f = new Footer(); //writer.PageEvent = f; document.Open(); PdfContentByte cb = writer.DirectContent; for (int i = 0; i < fileList.Length; i++) { if (fileList[i] != null) { reader = new PdfReader(fileList[i]); int iPageNum = reader.NumberOfPages; for (var pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++) { var page = writer.GetImportedPage(reader, pageNumber); var pageRotation = reader.GetPageRotation(pageNumber); r.Add(pageRotation); var isVertical = page.Width <= page.Height ? true : false; if (90 == pageRotation) { var p = PageSize.A4.Rotate(); p.Rotation = 270; document.SetPageSize(page.Width <= page.Height ? PageSize.A4 : p); } else { document.SetPageSize(page.Width <= page.Height ? PageSize.A4 : PageSize.A4.Rotate()); } document.NewPage(); var widthFactor = document.PageSize.Width / page.Width; var heightFactor = document.PageSize.Height / page.Height; var factor = Math.Min(widthFactor, heightFactor); var offsetX = (document.PageSize.Width - (page.Width * factor)) / 2; var offsetY = (document.PageSize.Height - (page.Height * factor)) / 2; cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY); } } } document.Close(); }
第二段 自動轉置PDF
public void RotatePDFFiles(string fileList, string outMergeFile, List<int> rotations) { PdfReader reader; Document document = new Document(); //MemoryStream ms = new MemoryStream(); using (var steam = new FileStream(outMergeFile, FileMode.Create)) { PdfWriter writer = PdfWriter.GetInstance(document, steam); //PdfWriter writer = PdfWriter.GetInstance(document, ms); var f = new Footer(); writer.PageEvent = f; document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; if (fileList != null) { reader = new PdfReader(fileList); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); newPage = writer.GetImportedPage(reader, j); var pageRotation = rotations[j-1]; writer.DirectContent.AddTemplate(newPage, 1f, 0, 0, 1f, 0, 0); } } document.Close(); } // var r = ms.ToArray(); // ms.Close(); }
呼,總算是達成任務...
沒有留言:
張貼留言