Rotate paragraphs or cells some arbitrary number o

2019-02-12 08:33发布

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);

2条回答
在下西门庆
2楼-- · 2019-02-12 09:03

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();
查看更多
小情绪 Triste *
3楼-- · 2019-02-12 09:09
  • 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.

查看更多
登录 后发表回答