Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating PDF in C# using iTextSharp

Tags:

c#

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(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();
         }
     }
like image 222
acadia Avatar asked Aug 26 '10 20:08

acadia


1 Answers

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 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));
}
like image 194
TimS Avatar answered Sep 21 '22 15:09

TimS