Rotate all pages by a given angle 2006-08-24 - By Sylvain Machefert
Back Hi !
I want to rotate all pages of a given PDF by 90, 180 or 270 degrees. I read the page about matrices http://itextdocs.lowagie.com/tutorial/directcontent/coordinates/index.html#top and try understand the matrix [ a, b, 0 c, d, 0 e, f, 1 ] [ sX * Math.cos(angle), sY * Math.sin(angle), 0 -sX * Math.sin(angle), sY * Math.cos(angle), 0 tX, tY, 1 ] What s, t, X and Y stand for ?
Here is my code. I tried various values for a, b, c, d, e and f, but never get a good rotation. I often get blank pages (content is out of the page), or not rotated but transformed (like a parallelogram), or a 45 degrees rotation... Never what I want :( Can you help me ? What are the value for the 3 angles ?
pAngle is my parameter 90, 180 or 270 degrees
FileOutputStream fos = new FileOutputStream(tmp);
//we create a reader for a certain document PdfReader reader = new PdfReader(sourcepdfbytes[]); //we retrieve the total number of pages int n = reader.getNumberOfPages();
//step 1: creation of a document-object Rectangle rect = reader.getPageSizeWithRotation(1); if (sens == ANTI_CLOCKWISE || sens == CLOCKWISE) rect=rect.rotate(); Document document = new Document(rect);
//step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.getInstance(document, fos); //step 3: we open the document document.open(); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page;
//Calcule values for the matrix //http://itextdocs.lowagie.com/tutorial/directcontent/coordinates/index.html #transform double angle = Math.PI; //Math.sin/cos want in radians switch(pAngle) { case 90: angle = Math.PI*90/180; break; case 180: angle = Math.PI*180/180; break; case 270: angle = Math.PI*270/180; break; } float a=(float) Math.cos(angle); float b=(float) Math.sin(angle); float c=(float) -Math.sin(angle); float d=(float) Math.cos(angle);
//step 4: we add content for (int i = 1; i <= n; i++) { rect = reader.getPageSizeWithRotation(i); if (pAngle == 90 || pAngle == 270) rect=rect.rotate(); document.setPageSize(rect); document.newPage(); page = writer.getImportedPage(reader, i);
cb.addTemplate(page, a, b, c, d, rect.width(), rect.height());
System.out.println("Processed page " + i); } //step 5: we close the document document.close();
Your help will be appreciated !
-- Sylvain Machefert http://iubito.free.fr http://tousauxbalkans.jexiste.fr
Hi !<br><br>I want to rotate all pages of a given PDF by 90, 180 or 270 degrees .<br>I read the page about matrices <a href="http://itextdocs.lowagie.com /tutorial/directcontent/coordinates/index.html#top">http://itextdocs.lowagie.com /tutorial/directcontent/coordinates/index.html#top </a> and try understand the matrix<br> [ a, b, 0<br> c, d, 0<br> e, f, 1 ]<br clear="all">[ sX * Math.cos (angle), sY * Math.sin(angle), 0 <br>-sX * Math.sin(angle), sY * Math.cos(angle) , 0<br>tX, tY, 1 ]<br>What s, t, X and Y stand for ? <br><br>Here is my code. I tried various values for a, b, c, d, e and f, but never get a good rotation. I often get blank pages (content is out of the page) , or not rotated but transformed (like a parallelogram), or a 45 degrees rotation... Never what I want :( <br>Can you help me ?<br>What are the value for the 3 angles ?<br> <br >pAngle is my parameter 90, 180 or 270 degrees <br><br> FileOutputStream fos = new FileOutputStream(tmp);<br><br> //we create a reader for a certain document <br> PdfReader reader = new PdfReader(sourcepdfbytes[]);<br> //we retrieve the total number of pages<br> int n = reader.getNumberOfPages();<br> <br> //step 1: creation of a document-object<br> Rectangle rect = reader.getPageSizeWithRotation(1);<br> if (sens == ANTI_CLOCKWISE || sens == CLOCKWISE)<br> rect=rect.rotate();<br> Document document = new Document (rect);<br> <br> //step 2: we create a writer that listens to the document <br> PdfWriter writer = PdfWriter.getInstance(document, fos);<br> //step 3: we open the document<br> document.open();<br> PdfContentByte cb = writer .getDirectContent();<br> PdfImportedPage page;<br><br> //Calcule values for the matrix <br> //http://itextdocs.lowagie.com/tutorial/directcontent/coordinates/index .html#transform<br> double angle = Math.PI; //Math.sin/cos want in radians<br> switch(pAngle) {<br> case 90: angle = Math.PI*90/180; break; <br> case 180: angle = Math.PI*180/180; break;<br>   ;case 270: angle = Math.PI*270/180; break;<br> }<br> float a=(float) Math .cos(angle);<br> float b=(float) Math.sin(angle);<br> float c=(float) -Math .sin(angle); <br> float d=(float) Math.cos(angle);<br> <br> //step 4: we add content <br> for (int i = 1; i <= n; i++) {<br> rect = reader .getPageSizeWithRotation(i);<br> if (pAngle == 90 || pAngle == 270)<br> rect= rect.rotate();<br> document.setPageSize(rect);<br> document.newPage();<br> page = writer.getImportedPage (reader, i);<br><br> cb.addTemplate(page, a, b, c, d, rect.width( ), rect.height());<br> <br> System.out.println("Processed page " + i);<br> }<br> //step 5: we close the document<br> document.close();<br> <br> Your help will be appreciated !<br><br>-- <br>Sylvain Machefert<br><a href="http://iubito.free .fr"> http://iubito.free.fr</a><br><a href="http://tousauxbalkans.jexiste.fr">http:/ /tousauxbalkans.jexiste.fr</a>
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 __ ____ ____ ____ ____ ____ ____ ____ ____ ____ iText-questions mailing list iText-questions@(protected) https://lists.sourceforge.net/lists/listinfo/itext-questions
|
|