Wednesday, January 9, 2008

Rotate All Pages in a PDF File

There are probably a few different ways to rotate pages in a PDF. I recently came across a helpful snippet from Paulo Soares that demonstrates one of these methods. (In case you are looking for a java example, one is included below the ColdFusion example).

The code is quite simple. You create a PdfReader object to read in the input file. Then loop through each page in the pdf and change the rotation using the page's dictionary. That is all there is to it.

What you need to run this example

For this example I used the ChapterSection.pdf sample file from the iText site. Download it and save it to the same directory as your .cfm script. You could also use your own pdf file if you prefer. Just remember to change the file paths in the sample code.

Note, the ColdFusion example will work on either MX7 or CF8 without any additional jars.

ColdFusion Example

<h1>Rotate All Pages Example</h1>

<cfscript>
savedErrorMessage = arrayNew(1);

// cfSearching: All file paths are relative to the current directory
fullPathToInputFile = ExpandPath("ChapterSection.pdf");
fullPathToOutputFile = ExpandPath("ChapterSectionRotated.pdf");
degreesToRotate = javacast("int", 90);

try {
// cfSearching: initialize required objects once and reuse
PdfName = createObject("java", "com.lowagie.text.pdf.PdfName");
PdfNumber = createObject("java", "com.lowagie.text.pdf.PdfNumber");

// cfSearching: read in the input pdf file
reader = createObject("java", "com.lowagie.text.pdf.PdfReader").init( fullPathToInputFile );

// cfSearching: rotate each page by the given number of degrees
for (p = 1; p LTE reader.getNumberOfPages(); p = p + 1) {
dictionary = reader.getPageN(javacast("int", p));
dictionary.put( PdfName.ROTATE, PdfNumber.init(degreesToRotate) );
WriteOutput("Rotating page "& p &" "& degreesToRotate &" degrees <br>");
}

// cfSearching: save a copy of the rotated pdf
outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
stamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(reader, outStream);

}
catch (com.lowagie.text.DocumentException de) {
savedErrorMessage = de;
}
catch (java.io.IOException ioe) {
savedErrorMessage = ioe;
}

// cfSearching: always close the stamper
if (IsDefined("stamper")) {
stamper.close();
}
if (IsDefined("outStream")) {
outStream.close();
}
WriteOutput("<hr>Finished!");
</cfscript>


<!--- show any errors --->
<cfif arrayLen(savedErrorMessage) gt 0>
Error creating file(s)
<cfdump var="#savedErrorMessage#">
</cfif>


Java Example

/*
* Rotate all pages in a PDF
*/
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfNumber;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfStamper;
import java.io.FileOutputStream;
import java.io.IOException;

public class RotatePages {
public static void main(String[] args) {
//the number of degrees rotation
int degrees = 90;
try {
System.out.println("Rotating all pages by "+ degrees +" degrees.");

PdfReader reader = new PdfReader("ChapterSection.pdf");
for (int p = 1; p <= reader.getNumberOfPages(); ++p) {
reader.getPageN(p).put(PdfName.ROTATE, new PdfNumber(degrees));
}
PdfStamper stp = new PdfStamper(reader, new FileOutputStream("ChapterSectionRotated.pdf"));
stp.close();

}
catch (DocumentException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

2 comments:

Raymond January 23, 2008 at 1:34 PM  

Do you mind if I add this to my pdfutils component on riaforge?

cfSearching January 23, 2008 at 1:45 PM  

No, I do not mind. The real source is from Paulo Soares of course :)

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep