予了下面函数的PDF分割成两个。 虽然它劈裂的PDF格式,内容是出现倒挂。我怎么用它旋转180度。 请帮助。下面是对代码
private static void ExtractPages(string inputFile, string outputFile, int start, int end) { // get input document PdfReader inputPdf = new PdfReader(inputFile); // retrieve the total number of pages int pageCount = inputPdf.NumberOfPages; if (end < start || end > pageCount) { end = pageCount; } // load the input document Document inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1)); // create the filestream using (FileStream fs = new FileStream(outputFile, FileMode.Create)) { // create the output writer PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs); inputDoc.Open(); PdfContentByte cb1 = outputWriter.DirectContent; // copy pages from input to output document for (int i = start; i <= end; i++) { inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1)); inputDoc.NewPage(); PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i); int rotation = inputPdf.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height); } else { cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } } inputDoc.Close(); } }
本文地址 :CodeGo.net/190700/ ------------------------------------------------------------------------------------------------------------------------- 1. 我想你的代码,它工作得很好splitting的网页保留着原来的方向。 一种解决方法可能是显式地旋转你的页面180度。 替换:
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
用:
cb1.AddTemplate(page, -1f, 0, 0, -1f, inputPdf.GetPageSizeWithRotation(i).Width, inputPdf.GetPageSizeWithRotation(i).Height);
如果你的电话inputPdf.GetPageRotation(i)
返回180,那么你可以处理这种在if
下面(使用我的推荐代码:旋转==180)。
2. 你应该试试这个。它的工作
if (rotation == 90 || rotation == 270) { if (rotation == 90) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height); } if (rotation == 270) { cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(pagenumber).Width, 0); } } else { cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); }
3. 我已经找到了所有4的主要轮换上面的答案不正确转动。 下面是我的代码正确处理0,90,180和270。这已经过测试,在每个这些方向旋转的PDF。
var pageRotation = reader.GetPageRotation(currentPageIndex); var pageWidth = reader.GetPageSizeWithRotation(currentPageIndex).Width; var pageHeight = reader.GetPageSizeWithRotation(currentPageIndex).Height; switch (pageRotation) { case 0: writer.DirectContent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0); break; case 90: writer.DirectContent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pageHeight); break; case 180: writer.DirectContent.AddTemplate(importedPage, -1f, 0, 0, -1f, pageWidth, pageHeight); break; case 270: writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0); break; default: throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}].", pageRotation)); }
4. 在上面的代码中的变化不大旧代码
case 270: writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
新代码
case 270: writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageHeight, 0);
这将解决与270度旋转的问题 本文标题 :使用iTextSharp的在C#中旋转PDF