iText paragraph.setRotationAngle 更改文本方向
iText paragraph.setRotationAngle changes text orientation
我正在尝试向 PDF 文件添加一些垂直文本,但是当我使用 setRotationAngle 旋转我的段落时,每 space 变成一个换行符或文本方向改变,我不确定.图片显示了我期望的结果和我得到的结果。我在 NetBeans 12.3 上使用 iText 7.1.0。
Result I want
Result I get
这是我的完整代码
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();
}
}```
尝试下面的代码,它将按预期工作 showTextAligned 是一种编写关于指定点对齐的文本的便捷方法
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);
完整代码
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();
}
我正在尝试向 PDF 文件添加一些垂直文本,但是当我使用 setRotationAngle 旋转我的段落时,每 space 变成一个换行符或文本方向改变,我不确定.图片显示了我期望的结果和我得到的结果。我在 NetBeans 12.3 上使用 iText 7.1.0。
Result I want
Result I get
这是我的完整代码
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();
}
}```
尝试下面的代码,它将按预期工作 showTextAligned 是一种编写关于指定点对齐的文本的便捷方法
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);
完整代码
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();
}