feat: 计算页边距
This commit is contained in:
parent
5978de5534
commit
cf8e27d18f
@ -30,6 +30,10 @@ import java.util.Map;
|
||||
public class PdfPrinter {
|
||||
|
||||
private static final Map<String, MediaSizeName> PAPER_SIZES = new HashMap<>();
|
||||
|
||||
// 装订边距常量(英寸)
|
||||
private static final double BINDING_MARGIN_LEFT = 0.5; // 左装订额外边距
|
||||
private static final double BINDING_MARGIN_TOP = 0.5; // 顶装订额外边距
|
||||
|
||||
static {
|
||||
PAPER_SIZES.put("Letter", MediaSizeName.NA_LETTER); // 8.5" × 11"
|
||||
@ -77,9 +81,7 @@ public class PdfPrinter {
|
||||
|
||||
String color = option.getColor();
|
||||
//如果是封面彩打 那么要把封面和 内容分开打印 就做两个打印任务 先打封面 再打内容
|
||||
if (color.equalsIgnoreCase("Cover Letter Color Only")){
|
||||
|
||||
|
||||
if ("Cover Letter Color Only".equals(color)) {
|
||||
PDDocument cover = new PDDocument();
|
||||
cover.addPage(document.getPage(0));
|
||||
cover.close();
|
||||
@ -87,7 +89,7 @@ public class PdfPrinter {
|
||||
PDDocument content = new PDDocument();
|
||||
for (int i = 0; i < document.getNumberOfPages(); i++) {
|
||||
//第一页是封面
|
||||
if (i==0){
|
||||
if (i == 0) {
|
||||
continue;
|
||||
}
|
||||
content.addPage(document.getPage(i));
|
||||
@ -99,7 +101,10 @@ public class PdfPrinter {
|
||||
option.setColor("black & white");
|
||||
setPageStyle(content, job, option);
|
||||
|
||||
}else {
|
||||
} else {
|
||||
if (StrUtil.containsIgnoreCase(color,"color")){
|
||||
option.setColor("color");
|
||||
}
|
||||
//全部打印
|
||||
setPageStyle(document, job, option);
|
||||
}
|
||||
@ -265,8 +270,8 @@ public class PdfPrinter {
|
||||
// 获取边距(默认为0.5英寸)
|
||||
double marginInches = option.getMargin();
|
||||
|
||||
// 创建并返回Paper对象
|
||||
return createPaper(dimensions[0], dimensions[1], marginInches);
|
||||
// 创建并返回Paper对象,考虑装订选项
|
||||
return createPaper(dimensions[0], dimensions[1], marginInches, option.getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -382,22 +387,52 @@ public class PdfPrinter {
|
||||
*
|
||||
* @param width 纸张宽度(点)
|
||||
* @param height 纸张高度(点)
|
||||
* @param marginInches 边距(英寸)
|
||||
* @param marginInches 基础边距(英寸)
|
||||
* @param bindingOption 装订选项
|
||||
* @return 配置好的Paper对象
|
||||
*/
|
||||
private static Paper createPaper(double width, double height, double marginInches) {
|
||||
private static Paper createPaper(double width, double height, double marginInches, String bindingOption) {
|
||||
Paper paper = new Paper();
|
||||
paper.setSize(width, height);
|
||||
|
||||
// 计算各边的边距,考虑装订选项
|
||||
double leftMargin = marginInches;
|
||||
double topMargin = marginInches;
|
||||
double rightMargin = marginInches;
|
||||
double bottomMargin = marginInches;
|
||||
|
||||
// 根据装订选项调整边距
|
||||
if (bindingOption != null) {
|
||||
switch (bindingOption) {
|
||||
case "Left":
|
||||
leftMargin += BINDING_MARGIN_LEFT;
|
||||
break;
|
||||
case "Top":
|
||||
topMargin += BINDING_MARGIN_TOP;
|
||||
break;
|
||||
case "Staple":
|
||||
leftMargin += BINDING_MARGIN_LEFT;
|
||||
topMargin += BINDING_MARGIN_TOP;
|
||||
break;
|
||||
case "None":
|
||||
default:
|
||||
// 不调整边距
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 将边距从英寸转换为点
|
||||
double marginPoints = marginInches * 72;
|
||||
double leftMarginPoints = leftMargin * 72;
|
||||
double topMarginPoints = topMargin * 72;
|
||||
double rightMarginPoints = rightMargin * 72;
|
||||
double bottomMarginPoints = bottomMargin * 72;
|
||||
|
||||
// 设置可打印区域
|
||||
paper.setImageableArea(
|
||||
marginPoints,
|
||||
marginPoints,
|
||||
width - 2 * marginPoints,
|
||||
height - 2 * marginPoints
|
||||
leftMarginPoints,
|
||||
topMarginPoints,
|
||||
width - leftMarginPoints - rightMarginPoints,
|
||||
height - topMarginPoints - bottomMarginPoints
|
||||
);
|
||||
|
||||
return paper;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user