当我尝试旋转时,创建图章注释,图章注释文本和矩形边框的示例代码显示错误。
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
时标记错误显示错误
答案 0 :(得分:0)
如果要将注释的内容旋转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();