0% found this document useful (0 votes)
3K views

Rotating PDF in C# Using iTextSharp

The document discusses programmatically rotating pages when splitting a PDF document into multiple files using iTextSharp in C#. Rotating the pages is necessary because splitting may result in pages appearing upside down. Several solutions are provided to explicitly rotate pages 0, 90, 180, and 270 degrees to properly handle page orientation during splitting.

Uploaded by

cafjnk
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Rotating PDF in C# Using iTextSharp

The document discusses programmatically rotating pages when splitting a PDF document into multiple files using iTextSharp in C#. Rotating the pages is necessary because splitting may result in pages appearing upside down. Several solutions are provided to explicitly rotate pages 0, 90, 180, and 270 degrees to properly handle page orientation during splitting.

Uploaded by

cafjnk
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Rotating PDF in C# using iTextSharp - Stack Overflow

http: stackoverflow!co" #uestions $%&'(%) rotating-p*!!!

sign up

log in

tour

help

careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Rotating PDF in C# using iTextSharp

I am using the below function to split the pdf into two. Though it is spliting the pdf, the content is appearing upside down. How do I rotate it by 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( )); // create the filestream using (Fi eStream fs = new Fi eStream(outputFile, Fi e!ode."reate)) { // create the output writer PdfWriter output!riter = PdfWriter.Get#nstance(inputDoc, fs); inputDoc.Open(); Pdf"ontent$%te c" = output!riter.Direct"ontent;

// copy pages from input to output document for (int i = start; i <= end; i##) { inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation( )); inputDoc.NewPage(); Pdf#mportedPage page =
c# itextsharp

asked Aug 26 '10 at 20:06 acadia 534 4 16 41

4 Answers
I tried your code and it worked fine for me; split pages kept their original orientation.

+ of $

, ,( ,(+- $:$' ./

Rotating PDF in C# using iTextSharp - Stack Overflow

http: stackoverflow!co" #uestions $%&'(%) rotating-p*!!!

A workaround might be to explicitly rotate your pages 180 degrees. Replace: c" .&dd'emp ate(page, With: c" .&dd'emp ate(page, % f, $, $, % f, inputPdf.GetPageSizeWithRotation(i).Width, inputPdf.GetPageSizeWithRotation(i).(eight); If your call to inputPdf.&etPage'otation(i) returns 180 then you can handle this in the if statement that follows (using my suggested code for rotation == 180).
answered Aug 27 '10 at 19:23 Jay Riggs 28.5k 5 48 77

f, $, $,

f, $, $);

You should try this. It worked for me: if (rotation == ($ || rotation == )*$) { if (rotation == ($) { c".&dd'emp ate(page, $, % f, f, $, $, reader.GetPageSizeWithRotation } if (rotation == )*$) { c".&dd'emp ate(page, $, .$F, % .$F, $, reader.GetPageSizeWithRotation } } e se { c".&dd'emp ate(page, }
answered Nov 16 '10 at 0:27 Jorge 61 1 2

f, $, $,

f, $, $);

I have found the above answers do not rotate correctly for all 4 of the main rotations. Below is my code to handle 0, 90, 180 and 270 correctly. This has been tested with a PDF rotated in each of these directions. var page'otation = reader.GetPageRotation(currentPage+nde,); var page!idt- = reader.GetPageSizeWithRotation(currentPage+nde,).Width; var page.eig-t = reader.GetPageSizeWithRotation(currentPage+nde,).(eight; switch (page'otation) { case $/ 0riter.Direct"ontent.&dd'emp ate(i1portedPage, f, $, $, f, $, $); brea); case ($/ 0riter.Direct"ontent.&dd'emp ate(i1portedPage, $, % f, brea); case

f, $, $, page.eig-t);

2$/ 0riter.Direct"ontent.&dd'emp ate(i1portedPage, % f, $, $, % f, page!idt-, page.eig-t

, of $

, ,( ,(+- $:$' ./

Rotating PDF in C# using iTextSharp - Stack Overflow


brea); case )*$/ 0riter.Direct"ontent.&dd'emp ate(i1portedPage, $, brea);

http: stackoverflow!co" #uestions $%&'(%) rotating-p*!!!

f, % f, $, page!idt-, $);

defau t/ throw new #nva idOperationException(string.Format(34ne,pected page rotation/ 5{$}6.3 }


answered Dec 18 '12 at 23:12 TimS 389 1 8

A little change in above code old code case )*$/ 0riter.Direct"ontent.&dd'emp ate(i1portedPage, $, new code case )*$/ 0riter.Direct"ontent.&dd'emp ate(i1portedPage, $, This will fix the issue with 270 degree rotation
edited Feb 27 '13 at 13:35 Eli Gassert 5,632 1 4 14 answered Feb 27 '13 at 13:10 dayanand 1

f, % f, $, page!idt-, $);

f, % f, $, page.eig-t, $);

Not the answer you're looking for !rowse other "uestions tagge# $# itextsharp or ask your own "uestion%

$ of $

, ,( ,(+- $:$' ./

You might also like