Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
JSP - A mailing list about Java Server Pages specification and reference
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Oracle Connection Pooling in 3 2 2
Servlet : Session invalidate
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Tomcat and webapplication specific java library path
Running a Simple JMS Example
Mapping in workers2 properties
org apache jasper JasperException
Cannot find message resources under key org apache struts action
   MESSAGE
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
url string for connecting jboss to oracle
Value attribute of <html:checkbox
javax servlet ServletException: BeanUtils populate
HTTP Status 404 The requested resource is not available
5 0 18: Windows XP Pro vs Windows 2000
 
Rotate all pages by a given angle

Rotate all pages by a given angle

2006-08-24       - By Sylvain Machefert

 Back
Reply:     1     2     3     4     5  

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>&nbsp;and&nbsp;try&nbsp;understand&nbsp;the&nbsp;matrix<br> [ a, b, 0<br>
&nbsp;&nbsp;c, d, 0<br>&nbsp;&nbsp;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&nbsp;?<br> <br
>pAngle&nbsp;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>       &nbsp;&nbsp;case 90: angle = Math.PI*90/180; break;
<br>       &nbsp;&nbsp;case 180: angle = Math.PI*180/180; break;<br>       &nbsp;&nbsp
;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 &lt;= n; i++) {<br>         &nbsp;&nbsp;rect = reader
.getPageSizeWithRotation(i);<br>         &nbsp;&nbsp;if (pAngle == 90 ||  pAngle ==
270)<br>           &nbsp;&nbsp;&nbsp;&nbsp;rect=
rect.rotate();<br>         &nbsp;&nbsp;document.setPageSize(rect);<br>         &nbsp;
&nbsp;document.newPage();<br>         &nbsp;&nbsp;page = writer.getImportedPage
(reader, i);<br><br>         &nbsp;&nbsp;cb.addTemplate(page, a, b, c, d, rect.width(
), rect.height());<br>         <br>&nbsp;&nbsp;
System.out.println(&quot;Processed page &quot; + i);<br>       }<br>       //step 5: we
close the document<br>       document.close();<br>&nbsp;&nbsp;<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

©2008 junlu.com - Jax Systems, LLC, U.S.A.