Rotate paragraphs or cells some arbitrary number of degrees — Itext

后端 未结 2 630
感情败类
感情败类 2021-01-04 12:01

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.

相关标签:
2条回答
  • 2021-01-04 12:16

    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();
    
    0 讨论(0)
  • 2021-01-04 12:29
    • 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.

    0 讨论(0)
提交回复
热议问题