Rotate paragraphs or cells some arbitrary number of degrees — Itext

不羁的心 提交于 2019-11-30 18:17:34
  • 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.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!