feat: 计算页边距
This commit is contained in:
parent
5978de5534
commit
cf8e27d18f
@ -31,6 +31,10 @@ public class PdfPrinter {
|
|||||||
|
|
||||||
private static final Map<String, MediaSizeName> PAPER_SIZES = new HashMap<>();
|
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 {
|
static {
|
||||||
PAPER_SIZES.put("Letter", MediaSizeName.NA_LETTER); // 8.5" × 11"
|
PAPER_SIZES.put("Letter", MediaSizeName.NA_LETTER); // 8.5" × 11"
|
||||||
PAPER_SIZES.put("Legal", MediaSizeName.NA_LEGAL); // 8.5" × 14"
|
PAPER_SIZES.put("Legal", MediaSizeName.NA_LEGAL); // 8.5" × 14"
|
||||||
@ -77,9 +81,7 @@ public class PdfPrinter {
|
|||||||
|
|
||||||
String color = option.getColor();
|
String color = option.getColor();
|
||||||
//如果是封面彩打 那么要把封面和 内容分开打印 就做两个打印任务 先打封面 再打内容
|
//如果是封面彩打 那么要把封面和 内容分开打印 就做两个打印任务 先打封面 再打内容
|
||||||
if (color.equalsIgnoreCase("Cover Letter Color Only")){
|
if ("Cover Letter Color Only".equals(color)) {
|
||||||
|
|
||||||
|
|
||||||
PDDocument cover = new PDDocument();
|
PDDocument cover = new PDDocument();
|
||||||
cover.addPage(document.getPage(0));
|
cover.addPage(document.getPage(0));
|
||||||
cover.close();
|
cover.close();
|
||||||
@ -100,6 +102,9 @@ public class PdfPrinter {
|
|||||||
setPageStyle(content, job, option);
|
setPageStyle(content, job, option);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
if (StrUtil.containsIgnoreCase(color,"color")){
|
||||||
|
option.setColor("color");
|
||||||
|
}
|
||||||
//全部打印
|
//全部打印
|
||||||
setPageStyle(document, job, option);
|
setPageStyle(document, job, option);
|
||||||
}
|
}
|
||||||
@ -265,8 +270,8 @@ public class PdfPrinter {
|
|||||||
// 获取边距(默认为0.5英寸)
|
// 获取边距(默认为0.5英寸)
|
||||||
double marginInches = option.getMargin();
|
double marginInches = option.getMargin();
|
||||||
|
|
||||||
// 创建并返回Paper对象
|
// 创建并返回Paper对象,考虑装订选项
|
||||||
return createPaper(dimensions[0], dimensions[1], marginInches);
|
return createPaper(dimensions[0], dimensions[1], marginInches, option.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -382,22 +387,52 @@ public class PdfPrinter {
|
|||||||
*
|
*
|
||||||
* @param width 纸张宽度(点)
|
* @param width 纸张宽度(点)
|
||||||
* @param height 纸张高度(点)
|
* @param height 纸张高度(点)
|
||||||
* @param marginInches 边距(英寸)
|
* @param marginInches 基础边距(英寸)
|
||||||
|
* @param bindingOption 装订选项
|
||||||
* @return 配置好的Paper对象
|
* @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 paper = new Paper();
|
||||||
paper.setSize(width, height);
|
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(
|
paper.setImageableArea(
|
||||||
marginPoints,
|
leftMarginPoints,
|
||||||
marginPoints,
|
topMarginPoints,
|
||||||
width - 2 * marginPoints,
|
width - leftMarginPoints - rightMarginPoints,
|
||||||
height - 2 * marginPoints
|
height - topMarginPoints - bottomMarginPoints
|
||||||
);
|
);
|
||||||
|
|
||||||
return paper;
|
return paper;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user