Upload
cafjnk
View
1.236
Download
4
Embed Size (px)
DESCRIPTION
Rotating PDF in C# Using iTextSharp
Citation preview
×
acadia534 4 16 41
4 Answers
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(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 =
c# itextsharp
asked Aug 26 '10 at 20:06
I tried your code and it worked fine for me; split pages kept their original orientation.
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.
Rotating PDF in C# using iTextSharp
sign up log in tour help careers 2.0
Rotating PDF in C# using iTextSharp - Stack Overflow http://stackoverflow.com/questions/3579058/rotating-pd...
1 of 3 2/20/2014 3:39 AM
Jay Riggs28.5k 5 48 77
Jorge61 1 2
A workaround might be to explicitly rotate your pages 180 degrees.
Replace:
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
With:
cb1.AddTemplate(page, -1f, 0, 0, -1f,
inputPdf.GetPageSizeWithRotation(i).Width,
inputPdf.GetPageSizeWithRotation(i).Height);
If your call to inputPdf.GetPageRotation(i) returns 180 then you can handle this in the if statementthat follows (using my suggested code for rotation == 180).
answered Aug 27 '10 at 19:23
You should try this. It worked for me:
if (rotation == 90 || rotation == 270)
{
if (rotation == 90)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation
}
if (rotation == 270)
{
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation
}
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
answered Nov 16 '10 at 0:27
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 ofthese 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
Rotating PDF in C# using iTextSharp - Stack Overflow http://stackoverflow.com/questions/3579058/rotating-pd...
2 of 3 2/20/2014 3:39 AM
TimS389 1 8
Eli Gassert5,632 1 4 14
dayanand1
break;
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
break;
default:
throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}]."
}
answered Dec 18 '12 at 23:12
A little change in above code 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 issue with 270 degree rotation
edited Feb 27 '13 at 13:35 answered Feb 27 '13 at 13:10
Not the answer you're looking for? Browse other questions tagged c# itextsharp or
ask your own question.
Rotating PDF in C# using iTextSharp - Stack Overflow http://stackoverflow.com/questions/3579058/rotating-pd...
3 of 3 2/20/2014 3:39 AM