尝试旋转itext5时,戳记注释文本和矩形边框显示错误

尝试旋转itext5时,戳记注释文本和矩形边框显示错误,itext,Itext,尝试旋转时,创建戳记批注、戳记批注文本和矩形边框显示错误的示例代码 Rectangle rectangle = new Rectangle(725 ,192,751,350); float w = rectangle.getWidth(), h = rectangle.getHeight(); PdfContentByte cb = stamper.getOverContent(page); PdfAnnotation annotation = PdfAnnotation.createFreeT

尝试旋转时,创建戳记批注、戳记批注文本和矩形边框显示错误的示例代码

Rectangle rectangle = new Rectangle(725 ,192,751,350);
float w = rectangle.getWidth(), h = rectangle.getHeight();
PdfContentByte cb = stamper.getOverContent(page);
PdfAnnotation annotation = PdfAnnotation.createFreeText(stamper.getWriter(), rectangle, annot.getText(), cb);
boolean baseFont = annot.getAnnotationType().equalsIgnoreCase(AnnotationConstants.AnnotationType.STAMP.name()) ? BaseFont.NOT_EMBEDDED : BaseFont.EMBEDDED;
PdfAppearance app = cb.createAppearance(w, h);
ColumnText ct = new ColumnText(app);
ct.setAlignment(Element.ALIGN_CENTER);
ct.setSimpleColumn(new Rectangle(w, h));
ct.setIndent(10, true);// 3% of margin
Font textFont = new Font(BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, baseFont));
textFont.setSize(19);
Phrase phrase = new Phrase("Mayank Pandey", textFont);
ct.setText(phrase);
ct.setSpaceCharRatio(400);
ct.go();
app.setColorFill(BaseColor.RED);
app.setFontAndSize(BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, baseFont), annot.getFontConfiguration().getFontSize());
app.moveText(10, 10);
app.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
app.setColorFill(new BaseColor(255, 0, 0, 50));
app.setColorStroke(BaseColor.RED);
app.rectangle(0, 0, w, h);
app.fillStroke();
annotation.setAppearance(PdfName.N, app);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setColor(BaseColor.YELLOW);
annotation.setBorder(new PdfBorderArray(4, 4, 4));
annotation.setRotate(90);
stamper.addAnnotation(annotation, page);
旋转pdf时戳记批注显示错误


如果要将注释内容旋转90°,则应使用切换的宽度和高度创建内容:

PdfAppearance app = cb.createAppearance(h, w); //!
ColumnText ct = new ColumnText(app);
ct.setAlignment(Element.ALIGN_CENTER);
ct.setSimpleColumn(new Rectangle(h, w)); //!
...
app.setColorFill(new BaseColor(255, 0, 0, 50));
app.setColorStroke(BaseColor.RED);
app.rectangle(0, 0, h, w); //!
app.fillStroke();

@真是太棒了!这通常通过接受答案来表示(单击左上角的勾号)。