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
-------------------------------------------------------------------------
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@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to