当我尝试旋转PDF

时间:2017-04-04 11:03:16

标签: itext

ellipse not properly drawn

我正在尝试在旋转的PDF上绘制椭圆,但椭圆的边框在中心显得粗细

示例代码:

PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setColor(getColor(annot.getBorderColor()));
// annotation.setBorder(new PdfBorderArray(2, 2, 2));
// annotation.setColor(getColor(annot.getBorderColor()));
annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID));
PdfContentByte cb = stamper.getOverContent(page);
if ((int) (orientation % 360) == 90 || (int) (orientation % 360) == 270)
{
    w = rect.getHeight();
    h = rect.getWidth();
}
else
{
    w = rect.getWidth();
    h = rect.getHeight();
}
PdfAppearance app = cb.createAppearance(w + 3.5f, h + 3.5f);
app.setColorStroke(getColor(annot.getBorderColor()));
app.setLineWidth(3.5);
app.ellipse(rect.getLeft() + 1.5, rect.getBottom() + 1.5, rect.getRight() - 1.5, rect.getTop() - 1.5);
app.stroke();
annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
stamper.addAnnotation(annotation, page);

1 个答案:

答案 0 :(得分:0)

正如在评论中假设的那样,iText的一个功能(对于旋转的页面,它试图假装给用户他有一个直立的坐标系,而不是旋转的坐标系)会妨碍你。这个不幸的是,功能只会影响注释本身的尺寸,而不会影响您为其创建的外观。

如果页面旋转90°或270°,则必须使用宽度和高度与您使用的注释尺寸相比切换的外观尺寸。

此外,在外观模板上绘制椭圆时,还必须考虑切换尺寸。问题和通过谷歌驱动器共享的示例代码中的代码忘记了这一点。

最后,不得使用PdfAppearance.setBoundingBox覆盖外观模板的切换尺寸。通过谷歌驱动器共享的示例代码中的代码就是这样做的。

错误示例

如果我在旋转的页面上创建一个带有外观的椭圆注释,如下所示:

Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150);

PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setColor(BaseColor.RED);
annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID));

PdfContentByte cb = stamper.getOverContent(1);
PdfAppearance app = cb.createAppearance(rect.getWidth(), rect.getHeight());
app.setColorStroke(BaseColor.RED);
app.setLineWidth(3.5);
app.ellipse( 1.5,  1.5, rect.getWidth() - 1.5, rect.getHeight() - 1.5);
app.stroke();
annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);

stamper.addAnnotation(annotation, 1);

CreateEllipse test testCreateEllipseAppearanceOnRotated

我确实得到了一个有趣的椭圆:

deformed ellipse

正确示例

如果我在旋转的页面上创建一个带有外观的椭圆注释,其切换尺寸如下:

Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150);

PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setColor(BaseColor.RED);
annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID));

PdfContentByte cb = stamper.getOverContent(1);
// switched appearance dimensions
PdfAppearance app = cb.createAppearance(rect.getHeight(), rect.getWidth());
app.setColorStroke(BaseColor.RED);
app.setLineWidth(3.5);
// draw ellipse using switched appearance dimensions
app.ellipse( 1.5,  1.5, rect.getHeight() - 1.5, rect.getWidth() - 1.5);
app.stroke();
annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);

stamper.addAnnotation(annotation, 1);

CreateEllipse test testCreateCorrectEllipseAppearanceOnRotated

我得到了预期的椭圆:

regular ellipse