Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate paragraphs or cells some arbitrary number of degrees -- Itext

I have a web site where the users upload photos and create photobooks. Also, they can add text at absolute positions, rotations, and alignments. The text can have new lines.

I've been using the Itext Library to automatize the creation of the Photobooks High Quality Pdfs that are printed latter on.

Adding the user uploaded images to the PDFs was really simple, the problem comes when I try to add the text.

In theory what I would need to do, is to define a paragraph of some defined width and height, set the users text, font, font style, alignment (center, left, right, justify), and finally set the rotation.

For what i've read about Itext, i could create a paragraph set the user properties, and use a ColumnText Object to set the absolute position, width and height. However it's not possibly to set the rotation of anything bigger than single line.

I can't use table cells either, because the rotation method only allow degrees that are multiples of 90.

Is there a way to add a paragraph with some rotation (say 20 degrees) without having to add the text line by line using the ColumnText.showTextAligned() method and all math that involves?

---- Edit: 08-Ago-2013 ----

If it helps anyone, this is the code I used to solve this problem (thanks to Bruno):

//Create the template that will contain the text
PdfContentByte canvas = pdfWriter.getDirectContent();
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight); //The width and height of the text to be inserted

ColumnText columnText = new ColumnText(textTemplate);

columnText.setSimpleColumn(0, 0, imgWidth, imgHeight);
columnText.addElement(paragraph);

columnText.go();

//Create de image wraper for the template
Image textImg = Image.getInstance(textTemplate);

//Asign the dimentions of the image, in this case, the text
textImg.setInterpolation(true);
textImg.scaleAbsolute(imgWidth, imgHeight);
textImg.setRotationDegrees((float) -textComp.getRotation()); //Arbitrary number of degress
textImg.setAbsolutePosition(imgXPos, imgYPos);

//Add the text to the pdf
pdfDocument.add(textImg);
like image 342
BernalCarlos Avatar asked Mar 14 '13 16:03

BernalCarlos


2 Answers

  • Create a PdfTemplate object; just a rectangle.
  • Draw your ColumnText on this PdfTemplate; don't worry about the rotation, just fill the rectangle with whatever content you want to add to the column.
  • Wrap the PdfTemplate inside an Image object; this is just for convenience, to avoid the math. This doesn't mean your text will be rasterized.
  • Now apply a rotation and an absolute position to the Image and add it to your document.

Your problem is now solved ;-)

PS: I'm the author of the iText in Action books.

like image 155
Bruno Lowagie Avatar answered Nov 06 '22 01:11

Bruno Lowagie


thanks to both our friends (Bruno & BernalCarlos) my final code for users that use "RTL" in their projects is here :

// step 1
Document document = new Document();
document.setPageSize(PageSize.A4);

// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destination_file));
CreateBorder event = new CreateBorder();
writer.setPageEvent(event);

// step 3
document.open();

// step 4
int imgWidth=400;
int imgHeight=50;
//Create the template that will contain the text
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight);
//The width and height of the text to be inserted

ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, imgWidth, imgHeight);
columnText.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
columnText.addElement(new Paragraph("محاسبه بار غیر متعادل", font_IranSemiBold));
columnText.go();

//Create de image wraper for the template
Image textImg = Image.getInstance(textTemplate);

//Asign the dimentions of the image, in this case, the text
textImg.setInterpolation(true);
textImg.scaleAbsolute(imgWidth, imgHeight);
textImg.setRotationDegrees(90); //Arbitrary number of degress
textImg.setAbsolutePosition(50, 200);

//Add the text to the pdf
document.add(textImg);

// step 5
document.close();
like image 39
Mohammadreza Soltani Avatar answered Nov 05 '22 23:11

Mohammadreza Soltani