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();