Java sets text rotation and skew in PDF

This paper introduces the method of setting text rotation and tilt in PDF document by Java program. When setting text sk...
This paper introduces the method of setting text rotation and tilt in PDF document by Java program. When setting text skew, you can define the method TransformText(page), and set page. Getcanvas(). Skewtransform (float angle, float angle);
When setting the text rotation, the text is rotated by defining the method RotateText(page) and setting page.getCanvas().rotateTransform(float angle). Refer to the following for detailed code examples. Note: you need to use the PDF class library tool Spire.PDF for Java Download jar package And unzip the jar file under the lib folder into the java program, or through the maven warehouse download and import.

Java example

import com.spire.pdf.*; import com.spire.pdf.graphics.*; import java.awt.*; public class TextStyle { public static void main(String[] args) { //establish PDF Documents, adding a page PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.appendPage(); //Set text skew TransformText(page); //Rotate text RotateText(page); //Save document pdf.saveToFile("DrawText.pdf",FileFormat.PDF); pdf.close(); } //Custom method adds text to the page and sets the text offset private static void TransformText(PdfPageBase page) { PdfGraphicsState state = page.getCanvas().save();//Save canvas state PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f);//Create fonts, including fonts, font sizes, etc //Create three brushes and assign colors PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(new Color(60,179,113))); PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(new Color(139,0,139))); PdfSolidBrush brush3 = new PdfSolidBrush(new PdfRGBColor(new Color(205,92,92))); page.getCanvas().translateTransform(20,120);//Set the coordinate position of the text in the canvas page.getCanvas().scaleTransform(1f,1f);//Set text size( scaleX The larger the value, the more the text is stretched horizontally to the right; scaleY The higher the value, the more vertically the text will stretch up) page.getCanvas().skewTransform(-10,0);//Set text deflection angle( angleX The value is the text overall offset angle, angleY Value is character offset angle) page.getCanvas().drawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0);//Draw text page.getCanvas().skewTransform(10, 10); page.getCanvas().drawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0);//Draw text page.getCanvas().scaleTransform(1f, -2f); page.getCanvas().drawString("Go! Turn Around! Go! Go! Go!", font, brush3, 0, -2 * 18);//Draw text //Save canvas state again page.getCanvas().restore(state); } //Custom method to add text to the page and set text rotation private static void RotateText(PdfPageBase page) { PdfGraphicsState state = page.getCanvas().save();//Save canvas state PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);//Create font PdfSolidBrush brush4 = new PdfSolidBrush(new PdfRGBColor(139,69,19));//Create brush 4 PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);//Set text alignment //Specify the coordinate position of the text in the canvas float x = (float) (page.getCanvas().getClientSize().getWidth()/2); float y = 390; page.getCanvas().translateTransform(x,y); //Draw 12 pieces of text content circularly, and set the interval of each text content to 30 degrees, that is, each text content rotates by 30 degrees with the last text content drawn (here, if you only need to draw a single text, you can directly specify the rotation angle) for (int i = 0; i < 12; i++) { page.getCanvas().rotateTransform(30); page.getCanvas().drawString("Go! Turn Around! Go! Go! Go!", font, brush4, 20, 0, centerAlignment); } //Save canvas state again page.getCanvas().restore(state); } }

Set result:

(end)

11 May 2020, 10:27 | Views: 3174

Add new comment

For adding a comment, please log in
or create account

0 comments