0

I'm using ItextPdf PdfSmartCopy and PdfDictionary. I've to detect the rotation of the input pdf file, based on rotation I've to decide how much should I rotate in order to bring it to zero degree(Normal view). Generally if the user scan the file, rotates it either left or right before saving, my program has to detect it when user uploads. I'm using the below code but for all the different types of files, rotation comes as zero only. Any idea how I can fix it?

    com.itextpdf.text.pdf.PdfReader existing = new com.itextpdf.text.pdf.PdfReader("c:\\pdf_issue\\rotate270.pdf");
        com.itextpdf.text.Document document1 = new com.itextpdf.text.Document();
        com.itextpdf.text.pdf.PdfCopy copy = new PdfSmartCopy(document1, new FileOutputStream("c:\\pdf_issue\\TestRotation270.pdf"));
        document1.open();
    int n1 = existing.getNumberOfPages();
        PdfDictionary pageDict;
        int rot;
        for (int i = 1; i <= n1; i++) {
            rot = existing.getPageRotation(i);

            switch(rot)
            {
            case 90:
               {
                pageDict = existing.getPageN(i);
                pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 270));
                 break;
               }
            case 180:
               {
                pageDict = existing.getPageN(i);
                pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 180));
                 break;            
}
            case 270:
               {
                pageDict = existing.getPageN(i);
                pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90));
               break;
}
            default:
               {
                pageDict = existing.getPageN(i);
                pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 0));
               break;
}
            }

        }
        for (int page = 0; page < n1; ) {
            copy.addPage(copy.getImportedPage(existing, ++page));
        }
    document1.close();
dev123
  • 11
  • 1
  • 5
  • But if I explicitly rotate a file without detecting the rotation, file gets rotated by whatever standard degree I give. – dev123 Feb 13 '15 at 11:19

1 Answers1

1

You may have to apply some heuristics to detect rotation. The problem is that you may have a page that originally was 5X10 and then rotate it by changing the page dimensions to 10X5 and keeping the page rotation indicator in the PDF as 0. The page is now landscape but the rotation is still 0. In some other PDF the page rotation indicator was changed to 90 keeping the page dimension at 5X10 and now you have a landscape that will show the same but it's different internally.

To be on the safe side check the rotation and the page size.

Paulo Soares
  • 1,896
  • 8
  • 21
  • 19
  • Assume that user will rotate either right or left, but as you said the rotation indicator is still zero. In this case the size will be 10x5 in both the cases, how can I find whether it's actually rotated in right or left? Coz I've to neutralize it accordingly to bring it to normal view – dev123 Feb 13 '15 at 12:55
  • You may have a look at the transformation matrix in the stream content to check if any rotation was applied but it won't work all the times, hence the heuristics. – Paulo Soares Feb 13 '15 at 13:22