Created
November 28, 2011 21:49
-
-
Save ashwanthkumar/1402243 to your computer and use it in GitHub Desktop.
Correct the page layout in Raj Kamal book, available on the internet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import com.itextpdf.text.DocumentException; | |
import com.itextpdf.text.pdf.PdfDictionary; | |
import com.itextpdf.text.pdf.PdfName; | |
import com.itextpdf.text.pdf.PdfNumber; | |
import com.itextpdf.text.pdf.PdfReader; | |
import com.itextpdf.text.pdf.PdfStamper; | |
/** | |
* A simple class to fix the RajKamal eBook that is available on the internet. | |
* | |
* @author Ashwanth Kumar <ashwanth@ashwanthkumar.in> | |
* @date 29/11/2011 | |
* | |
* $ javac -cp .;itextpdf-5.1.3.jar RajKamalBookCorrectPages.java | |
* $ java -cp .;itextpdf-5.1.3.jar RajKamalBookCorrectPages | |
**/ | |
public class RajKamalBookCorrectPages { | |
/** | |
* Manipulates the PDF file with src as original err'd book path and dest as the correct'd book path | |
* @param src the original PDF | |
* @param dest the resulting PDF | |
* @throws IOException | |
* @throws DocumentException | |
*/ | |
public void correctRajKamalSecondEditionBook(String src, String dest) | |
throws IOException, DocumentException { | |
PdfReader reader = new PdfReader(src); | |
int n = reader.getNumberOfPages(); | |
int rot; | |
PdfDictionary pageDict; | |
// Rotate all the even pages on the book | |
for (int i = 2; i <= n;) { | |
rot = reader.getPageRotation(i); | |
pageDict = reader.getPageN(i); | |
pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 180)); | |
i += 2; | |
} | |
// try to correct the landscape view to Portrait | |
for (int i = 1; i <= n; i++) { | |
rot = reader.getPageRotation(i); | |
pageDict = reader.getPageN(i); | |
pageDict.put(PdfName.ROTATE, new PdfNumber(rot - 90)); | |
} | |
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); | |
stamper.close(); | |
} | |
public static void main(String args[]) { | |
try { | |
new RajKamalBookCorrectPages().correctRajKamalSecondEditionBook("./EmbeddedSystems_RajKamal.pdf", "./corrected_Embedded_Systems_RajKamal.pdf"); | |
} catch (Exception e) { | |
System.out.println("error " + e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment