遇到一份PDF有橫有直
並且合併過程中發現橫的PDF會被切掉
因此要判斷是否為橫的PDF 並轉直在合併
public void Rotate(PdfReader reader, string outMergeFile) { int pageCount = reader.NumberOfPages; Document document = new Document(); //MemoryStream outputStream = new MemoryStream();//要把PDF寫到哪個串流 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create)); document.Open(); iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent; iTextSharp.text.pdf.PdfImportedPage page; int rotation; int i = 0; while (i < pageCount) { i++; var pageSize = reader.GetPageSizeWithRotation(i); // Pull in the page from the reader page = writer.GetImportedPage(reader, i); var pageWidth = pageSize.Width; var pageHeight = pageSize.Height; // 判斷是否為橫的PDF,並轉置 if (pageWidth > pageHeight) { var newPageSize = new Rectangle(pageSize.Height, pageSize.Width, 90); writer.DirectContent.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height); } else { writer.DirectContent.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.Close(); }