Rotate PDF in C # with iTextSharp

I use the following function to split a PDF into two.

Although it shares the pdf, the content appears upside down. How to rotate it 180 degrees.

Please, help. below is the code for the same

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(); } } 
+9
c # itextsharp
Aug 26 '10 at 20:06
source share
5 answers

I tried your code and it worked perfectly for me; divided pages retained their original orientation.

A workaround may be to explicitly rotate your pages 180 degrees.

Replace:

 cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); 

FROM

 cb1.AddTemplate(page, -1f, 0, 0, -1f, inputPdf.GetPageSizeWithRotation(i).Width, inputPdf.GetPageSizeWithRotation(i).Height); 

If your call to inputPdf.GetPageRotation(i) returns 180, you can handle this in the following if (using my suggested code to rotate == 180).

+2
Aug 27 '10 at 19:23
source share

I found that the answers above do not rotate correctly for all 4 main revolutions.

Below my code handles 0, 90, 180 and 270 correctly. This has been verified with a PDF rotated in each of these directions.

 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)); } 
+17
Dec 18 '12 at 23:12
source share

You must try this. This worked for me:

  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); } 
+6
Nov 16 '10 at 0:27
source share

@TimS 'answer was very close to perfection and provided the correct AddTemplate options, but I needed to make a few additions to allow PDF rotation, where pages already rotate 90, 180, 270 0, 90, 180 or 270:

Assuming that the RotateFlipType rotateFlipType parameter RotateFlipType rotateFlipType passed to a function to indicate rotation (a convenient enumeration from a GDI + RotateFlip call):

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

Hope this helps someone else who wants to fix the rotation of incoming PDF files. Took me 2 days to improve it.

+2
Oct 28 '14 at 12:04 on
source share

A slight change in the above code is the old code

 case 270: writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0); 

new code

 case 270: writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageHeight, 0); 

This will fix the 270 degree rotation problem.

0
Feb 27 '13 at 13:10
source share



All Articles