java - 用于矩形旋转的 Java pdfbox 库
问题描述
尝试使用基于 fabric json 的 pdfbox 库旋转矩形,但 x 坐标和 y 坐标未显示在正确的位置。
下面的代码:
public void drawRectangle(PDPageContentStream contentStream, PDPage page, JSONObject props) throws IOException {
String originX = (String) props.get("originX");
String originY = (String) props.get("originY");
Double left = (Double) props.get(originX);
Double top = (Double) props.get(originY);
Double width = (Double) props.get("width");
Double hight = (Double) props.get("height");
String fillColor = (String) props.get("fill"); // "#B3B3B3";
String strokeColor = null;
if (hight != 1.0) {
strokeColor = (String) props.get("stroke"); // "#000000";
}
Double strokeWidth = (Double) props.get("strokeWidth"); // 0.0f;
String strokeLineCap = (String) props.get("strokeLineCap"); // "butt";
String strokeLineJoin = (String) props.get("strokeLineJoin"); // "miter";
Double strokeMitterLimit = (Double) props.get("strokeMiterLimit");
String fillRule = (String) props.get("fillRule"); // "nonzero";
contentStream.setNonStrokingColor(Color.decode(fillColor));
if (hight != 1.0) {
contentStream.setStrokingColor(Color.decode(strokeColor));
}
contentStream.setLineWidth(strokeWidth.floatValue());
contentStream.setLineCapStyle(
"butt".equalsIgnoreCase(strokeLineCap) ? 0 : ("round".equalsIgnoreCase(strokeLineCap) ? 1 : 2));
contentStream.setLineJoinStyle(
"miter".equalsIgnoreCase(strokeLineJoin) ? 0 : ("round".equalsIgnoreCase(strokeLineCap) ? 1 : 2));
contentStream.setMiterLimit(strokeMitterLimit.floatValue());
Double scaleX = (Double) props.get("scaleX"); // 1.0f;//must be minimum 1
if (scaleX <= 1) {
scaleX = (Double) 1.0;
}
Double scaleY = (Double) props.get("scaleY"); // 1.0f;// must be minimum 1
if (scaleY <= 1) {
scaleY = (Double) 1.0;
}
double angle = (double) props.get("angle"); // 0.0d;
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
contentStream.transform(Matrix.getTranslateInstance(tx, ty));
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(360-angle), 0, 0));
contentStream.transform(Matrix.getTranslateInstance(-tx, -ty));
contentStream.addRect(left.floatValue(), page.getMediaBox().getHeight()-top.floatValue() - hight.floatValue(),
width.floatValue(), hight.floatValue());
if ("nonzero".equals(fillRule)) {
contentStream.fillAndStroke();
} else {
contentStream.fillAndStrokeEvenOdd();
}
}
我的示例织物 json:
{
"type": "rect",
"pos": 3,
"tag": "",
"version": "4.3.1",
"originX": "left",
"originY": "top",
"left": 550.6,
"top": 632.35,
"width": 71.26,
"height": 99.51,
"fill": "#B3B3B3",
"stroke": "#000000",
"strokeWidth": 0.0,
"strokeLineCap": "butt",
"strokeDashOffset": 0.0,
"strokeLineJoin": "miter",
"strokeMiterLimit": 4.0,
"scaleX": 1.0,
"scaleY": 1.0,
"angle": 111.31,
"flipX": false,
"flipY": false,
"opacity": 1.0,
"visible": true,
"backgroundColor": "",
"fillRule": "nonzero",
"paintFirst": "fill",
"globalCompositeOperation": "source-over",
"skewX": 0.0,
"skewY": 0.0,
"lockMovementX": false,
"lockMovementY": false,
"lockScalingX": false,
"lockScalingY": false,
"lockUniScaling": false,
"lockRotation": false,
"lockObject": false,
"fillColor": {
"id": 0,
"cyan": 0,
"magenta": 0,
"yellow": 0,
"black": 30,
"tint": 100.0,
"shade": 0.0,
"name": "C=0 M=0 Y=0 K=30",
"isTransparent": false,
"isSpotColor": false
},
"strokeColor": {
"id": 0,
"cyan": 0,
"magenta": 0,
"yellow": 0,
"black": 100,
"tint": 100.0,
"shade": 0.0,
"name": "C=0 M=0 Y=0 K=100",
"isTransparent": false,
"isSpotColor": false
},
"rx": 0.0,
"ry": 0.0,
"absolutePositioned": false,
"inverted": false
}
并尝试了stackoverflow中提到的不同方法,似乎没有任何效果。我感谢任何帮助。
解决方案
推荐阅读
- arrays - 展平 simd_float4x4 矩阵的优雅方法
- cocoa - CAOpenGLLayer 到图像
- python-2.7 - 是否可以在另一个三元运算符中使用三元运算符?
- kubernetes - 如何在 kubernetes 上设置基本的 rabbitmq
- java - 我如何加载 3d 模型并获取点的模型?
- c# - 无法访问连接字符串
- javascript - 如何从鼠标右键单击的上下文菜单中捕获选择?
- javascript - 当link_to remote:true时,如何返回生成的excel文件进行下载?
- javascript - 在“./n”中找不到导出“默认”(导入为“n”)
- protocols - 自制通信协议的校验和