I’m trying to get PDFBox to rotate a PDF document 270 from 90 and save.. I have had some help from this community on a few errors as I am new to java. There wasn’t really a good working example that I could try/follow.. I try to run the code and get the following errors:
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at PdfRotator.main(PdfRotator.java:36)
Code:
//import java.awt.List;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class PdfRotator {
private static final String pdfFile = null;
public static void main(String[] args) throws IOException {
PDDocument document = PDDocument.load("PDFrotatorTEST.pdf");
//public static void main(String[] args) throws Exception {
List pages = (List) document.getDocumentCatalog().getAllPages();
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) ((java.util.List) pages).get(i);// PDPage page = (PDPage) pages.get(i);
if ((i + 1) % 2 == 0) {
page.setRotation(0);
}
}
File f = new File(pdfFile);// File f = new File(pdfFile);
String newFile = f.getParent() + File.separator + "out.pdf";
try {
document.save(newFile);
} catch (COSVisitorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
}
Is the Catch block right? I’m not sure why I would really need that.. Any help with this is greatly appreciated!