Home > database >  How to rotate a rectangle having text as content to some angle let say 90' in counterclockwise
How to rotate a rectangle having text as content to some angle let say 90' in counterclockwise

Time:07-19

I have to a rectangle with some context as text in it. I have to rotate the rectangle to some angle lets say 90' anti clockwise such that the content also gets rotated. However when i am writing this code, it only shows a single rectangle.

This is the code:

package main;

import java.io.FileNotFoundException;

import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;

public class main {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        String dest = "C:\\Users\\beast\\Desktop\\samplePdf";
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        PdfPage page = pdf.addNewPage();
        PdfCanvas pdfCanvas = new PdfCanvas(page);
        Rectangle rect1 = new Rectangle(183, 488, 180, 32);
//      cordinates after rotation 90' counter clockwise
        Rectangle rect2 = new Rectangle(151, 488, 32, 180);
        pdfCanvas.rectangle(rect1);
        pdfCanvas.stroke();
        Canvas canvas = new Canvas(pdfCanvas, pdf, rect1);
        Text title = new Text("Thbvhs ybhsvb");
        Paragraph p = new Paragraph().add(title);
        canvas.add(p);
        AffineTransform transform=AffineTransform.getRotateInstance((float)(Math.PI/2));
        pdfCanvas.concatMatrix(transform);
        canvas.close();
        canvas=new Canvas(pdfCanvas, pdf, rect2);
        pdfCanvas.rectangle(rect2);
        pdfCanvas.stroke();
        canvas.add(p);
        canvas.close();
        pdf.close();

    }

}

this is the output of the pdf:

enter image description here

CodePudding user response:

With your introduction of the AffineTransform transform you already were on the right path. But you have to apply the rotation before adding the content you want to be rotated. Transformations here don't change the content already added, they change the coordinate system for the content to come.

Additionally you need to make transform rotate around a sensible rotation center. Your instance rotates around the origin of the coordinate system, i.e. the lower left corner of your page, but you apparently want to rotate around the lower left corner of your rectangle.

Thus:

try (PdfDocument pdf = new PdfDocument(new PdfWriter("RotatedForAnkushGupta.pdf"))) {
    PdfPage page = pdf.addNewPage();
    PdfCanvas pdfCanvas = new PdfCanvas(page);

    Rectangle rect1 = new Rectangle(183, 488, 180, 32);

    AffineTransform transform = AffineTransform.getRotateInstance(Math.PI/2, rect1.getX(), rect1.getY());
    pdfCanvas.concatMatrix(transform);

    pdfCanvas.rectangle(rect1);
    pdfCanvas.stroke();

    try (Canvas canvas = new Canvas(pdfCanvas, rect1)) {
        Text title = new Text("Thbvhs ybhsvb");
        Paragraph p = new Paragraph().add(title);
        canvas.add(p);
    }
}

(screenshot

  • Related