Search code examples
javapdfitextpdf-generation

iText paragraph.setRotationAngle changes text orientation


I'm trying to add some vertical texts to a PDF file, but when I rotate my paragraphs with setRotationAngle every space turn into a line break or the text orientation is changed, I'm not sure. The pictures show the result I expect and the result I'm getting. I'm using iText 7.1.0 at NetBeans 12.3.

Result I want

Result I get

Here's my full code

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
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 java.io.File;
import java.io.IOException;
import java.lang.Math;


public class assinatura {
    public static void main(String args[]) throws IOException {
        System.out.println("Assinando documento");
        assinar("hello.pdf", "first string", "second text");
    }

    public static void assinar(String documento, String txtAssinaturaA, String txtAssinaturaB) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfReader(documento),
                                          new PdfWriter(documento + "_assinado.pdf"));
        
        PdfPage primeiraPag = pdf.getFirstPage();

        PdfCanvas pdfCanvas = new PdfCanvas(primeiraPag);
        Rectangle rectangle = new Rectangle(550, 0, 550, 400);
        pdfCanvas.rectangle(rectangle);
        Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        Text txtA = new Text(txtAssinaturaA)
            .setFont(font)
            .setFontSize(10)
            .setFontColor(ColorConstants.BLUE);
        Text txtB = new Text(txtAssinaturaB)
            .setFont(font)
            .setFontSize(10)
            .setFontColor(ColorConstants.BLUE);
        
        Paragraph paragrafoA = new Paragraph().add(txtA);
        Paragraph paragrafoB = new Paragraph().add(txtB);
        paragrafoA.setRotationAngle(Math.toRadians(90));
        paragrafoB.setRotationAngle(Math.toRadians(90));

        canvas.add(paragrafoA);
        canvas.add(paragrafoB);
        canvas.close();
        pdf.close();

    }
}```

Solution

  • Try Below code it will work as expected showTextAligned is a Convenience method to write a text aligned about the specified point

    document.showTextAligned(paragrafoA.setFontColor(Color.BLUE), x, y , 1, TextAlignment.RIGHT, VerticalAlignment.TOP, (float) Math.PI / 2f);
         document.showTextAligned(paragrafoB.setFontColor(Color.BLUE), x+20, y , 1,
                  TextAlignment.RIGHT, VerticalAlignment.TOP, (float) Math.PI / 2f);
    

    Full code

    public static  void assinar(String documento, String txtAssinaturaA, String txtAssinaturaB) throws IOException {
          //Initialize PDF document
          PdfWriter pdfwriter=new PdfWriter(documento + "_assinado.pdf");
          PdfDocument pdfDocument = new PdfDocument(new PdfReader(documento),pdfwriter);
          pdfDocument.setDefaultPageSize(PageSize.A4);
          Document document=new Document(pdfDocument);
    
          //PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
          Text txtA = new Text(txtAssinaturaA)
                  .setFontSize(10)
                  .setFontColor(Color.BLUE);
          Text txtB = new Text(txtAssinaturaB)
                  .setFontSize(10)
                  .setFontColor(Color.BLUE);
        Paragraph paragrafoA = new Paragraph().add(txtA);
      Paragraph paragrafoB = new Paragraph().add(txtB);
    //here x and y is poinying to center so change 
          float x = pdfDocument.getDefaultPageSize().getWidth() / 2;
          float y = pdfDocument.getDefaultPageSize().getHeight() / 2;
       document.showTextAligned(paragrafoA.setFontColor(Color.BLUE), x, y , 1, TextAlignment.RIGHT, VerticalAlignment.TOP, (float) Math.PI / 2f);
         document.showTextAligned(paragrafoB.setFontColor(Color.BLUE), x+20, y , 1,
                  TextAlignment.RIGHT, VerticalAlignment.TOP, (float) Math.PI / 2f);
    
          document.close();
          pdfDocument.close();
    
       }