[]
        
(Showing Draft Content)

自定义边框样式

GcExcel 允许您在导出 PDF 文档时使用自定义边框样式,通过使用 PdfSaveOptions 类的 getBorderOptions 方法来实现。这个方法利用了 CustomBorderStyle 类中的 setBorderWidth setDashes 属性以及 BorderLineStyle 枚举,以设置边框的宽度、虚线段的长度和线条样式。setBorderWidth 方法在导出 PDF 文档时调整边框的宽度,而 setDashes 方法则调整虚线中每一段的长度。BorderLineStyle 枚举则指定了边框的线条样式。

下表列出了所有边框样式的默认值:

线型

默认线宽(point)

虚线 (point)

备注

Hair

0.2

None

-

DashDotDot

1

9,3,3,3,3,3

-

DashDot

1

8,2,2,2

-

Dotted

1

1,1

-

Dashed

1

3,1

-

Thin

1

None

-

MediumDashDotDot

2

4.5,1.5,1.5,1.5,1.5,1.5

-

SlantDashDot

1

11, 1,5,1

这种边框类型由两条线组成,每条线的宽度均为 1 point。

MediumDashDot

2

4.5,1.5,1.5,1.5

-

MediumDashed

2

4.5,1.5

-

Medium

2

None

-

Thick

3

None

这种边框类型由三条线组成,每条线的宽度均为 1 point。

Double

1

None

这种边框类型由两条线组成,每条线的宽度均为 1 point。

参考以下示例代码,了解如何在导出到 PDF 文档时调整边框宽度、虚线长度和线条样式:

// Create a new workbook.
Workbook workbook = new Workbook();
        
// Open template file.
workbook.open("CustomBorderStyle.xlsx");

// Customize the border style for PDF export.
// Initialize PdfSaveOptions.
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
        
// Set outer border width to 0.4.
CustomBorderStyle thinBorderSetting = new CustomBorderStyle();
thinBorderSetting.setBorderWidth(0.4);
        
// Set middle border width to 1.5.
CustomBorderStyle middleBorderSetting = new CustomBorderStyle();
middleBorderSetting.setBorderWidth(1.5);
        
// Set inner horizontal border width to 0.4 in dash style.
CustomBorderStyle dashBorderSetting = new CustomBorderStyle();
dashBorderSetting.setBorderWidth(0.4);
dashBorderSetting.setDashes(new ArrayList<>(Arrays.asList(0.8, 0.8)));
        
// Add borders with custom border styles.
pdfSaveOptions.getBorderOptions().put(BorderLineStyle.Thin, thinBorderSetting);
pdfSaveOptions.getBorderOptions().put(BorderLineStyle.Medium, middleBorderSetting);
pdfSaveOptions.getBorderOptions().put(workbook.getActiveSheet().getRange("B13").getBorders().get(BordersIndex.EdgeTop).getLineStyle(), dashBorderSetting);
        
// Save workbook to PDF file.
workbook.save("CustomBorder.pdf", pdfSaveOptions);