使用iTextSharp在C#中旋转PDF

我使用以下函数将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  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(); } } 

我尝试了你的代码,它对我来说很好; 拆分页面保持其原始方向。

解决方法可能是将页面显式旋转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,那么您可以在inputPdf.GetPageRotation(i)if语句中处理此问题(使用我建议的旋转代码== 180)。

我发现上述答案不能正确旋转所有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)); } 

你应该试试这个。 它对我有用:

  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); } 

@TimS的答案非常接近完美,并为AddTemplate提供了正确的参数,但是我需要做一些补充以允许PDF的AddTemplate旋转,其中页面已经旋转AddTemplate或270:

假设RotateFlipType rotateFlipType的参数RotateFlipType rotateFlipType被传递给函数以指定旋转(来自GDI + RotateFlip调用的方便枚举):

 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); // Get current page rotation in degrees rotation = pageSize.Rotation; // Default to the current page size iTextSharp.text.Rectangle newPageSize = null; // Apply our additional requested rotation (switch height and width as required) switch (rotateFlipType) { case RotateFlipType.RotateNoneFlipNone: newPageSize = new iTextSharp.text.Rectangle(pageSize); break; case RotateFlipType.Rotate90FlipNone: rotation += 90; newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation); break; case RotateFlipType.Rotate180FlipNone: rotation += 180; newPageSize = new iTextSharp.text.Rectangle(pageSize.Width, pageSize.Height, rotation); break; case RotateFlipType.Rotate270FlipNone: rotation += 270; newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation); break; } // Cap rotation into the 0-359 range for subsequent check rotation %= 360; document.SetPageSize(newPageSize); document.NewPage(); // based on the rotation write out the page dimensions switch (rotation) { case 0: cb.AddTemplate(page, 0, 0); break; case 90: cb.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height); break; case 180: cb.AddTemplate(page, -1f, 0, 0, -1f, newPageSize.Width, newPageSize.Height); break; case 270: cb.AddTemplate(page, 0, 1f, -1f, 0, newPageSize.Width, 0); break; default: throw new System.Exception(string.Format("Unexpected rotation of {0} degrees", rotation)); break; } } 

希望这将有助于其他希望纠正传入PDF文件轮换的人。 花了我2天时间来完善它。

上面的代码旧代码稍有变化

 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度旋转的问题