我正在使用Boxable库创建PDF文件,在其中打印数字序列。 PDF中的每一页将有10行和5列,在第10行之后,将添加一个新页面以打印其余的数字,依此类推。这是代码,可以正常工作。
public void createPdfColumnWise() throws IOException {
float margin = 20;
int firstNumber = 105;
int lastNumber = 1005;
int[] intArray = IntStream.range(firstNumber, lastNumber + 1).toArray();
PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
BaseTable table = pdfTable(margin, doc, page);
int j = 0;
int row = 0;
while (j < intArray.length) {
row ++;
if(row > 10) {
//if(row > 40) {
table.removeAllBorders(true);
table.draw();
row = 1;// reset
page = new PDPage(PDRectangle.A4);
doc.addPage(page);
table = pdfTable(margin, doc, page);
}
Row<PDPage> imageRow = table.createRow(25f);
Cell<PDPage> cell = imageRow.createCell(8f, "");
cell.setFontSize(6f);
cell = imageRow.createCell(10f,row + ")");
cell.setFontSize(6f);
for (int i = 0; i < 5; i++) {
if (j >= intArray.length) break;
String textValue = String.valueOf(intArray[j]);
cell = imageRow.createCell(7f, textValue, HorizontalAlignment.RIGHT, VerticalAlignment.MIDDLE);
cell.setTopPadding(6f);
cell.setBottomPadding(6f);
cell.setFontSize(12f);
j++;
}
}
table.removeAllBorders(true);
table.draw();
String userHome = System.getProperty("user.home");
File file = new File(userHome + "/Downloads/" + "Output.pdf");
Files.createParentDirs(file);
doc.save(file);
doc.close();
}
private BaseTable pdfTable(float margin, PDDocument doc, PDPage page) throws IOException {
float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
float yLocationNewPage = page.getMediaBox().getHeight() - (2 * margin);
boolean drawContent = true;
boolean drawLines = true;
float yStartPoint = yLocationNewPage + 50;
float bottomMargin = 15;
yStartPoint -= FontUtils.getHeight(PDType1Font.COURIER, 14) + 15;
BaseTable table = new BaseTable(yStartPoint, yLocationNewPage, bottomMargin, tableWidth, margin, doc,
page, drawLines, drawContent);
return table;
}
现在这将打印如下所示的值:
现在,我想知道是否可以按以下所示的格式打印值,以使数字序列按列递增:
所以我需要一种逻辑,即按行打印值,但按列打印数字序列。到目前为止,我能够使第一行正确,但是从第二行我无法使它起作用:
public void createPdfColumnWise() throws IOException {
float margin = 20;
int firstNumber = 105;
int lastNumber = 1005;
int[] intArray = IntStream.range(firstNumber, lastNumber + 1).toArray();
Stream<Integer> stream = Arrays.stream(intArray).boxed();
Integer lastNum = stream.reduce((a, b) -> b).orElse(null);
PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
BaseTable table = pdfTable(margin, doc, page);
int j = 0;
int row = 0;
while (j < intArray.length) {
row ++;
if(row > 10) {
//if(row > 40) {
table.removeAllBorders(true);
table.draw();
row = 1;// reset
page = new PDPage(PDRectangle.A4);
doc.addPage(page);
table = pdfTable(margin, doc, page);
}
Row<PDPage> imageRow = table.createRow(25f);
Cell<PDPage> cell = imageRow.createCell(8f, "");
cell.setFontSize(6f);
cell = imageRow.createCell(10f,row + ")");
cell.setFontSize(6f);
for (int i = 0; i < 5; i++) {
if (j >= intArray.length) break;
String textValue = null;
if(i != 0 && (intArray[j] + 9) < lastNum) {
textValue = String.valueOf(intArray[j] + ( 9 * i));
} else {
textValue = String.valueOf(intArray[j] );
}
cell = imageRow.createCell(7f, textValue, HorizontalAlignment.RIGHT, VerticalAlignment.MIDDLE);
cell.setTopPadding(6f);
cell.setBottomPadding(6f);
cell.setFontSize(12f);
j++;
}
}
table.removeAllBorders(true);
table.draw();
String userHome = System.getProperty("user.home");
File file = new File(userHome + "/Downloads/" + "Output.pdf");
Files.createParentDirs(file);
doc.save(file);
doc.close();
}
要使用boxable,您需要在POM中具有以下依赖性:
<dependency>
<groupId>com.github.dhorions</groupId>
<artifactId>boxable</artifactId>
<version>1.5</version>
</dependency>
答案 0 :(得分:0)
让我给你一个例子:
0
您如何看待这个想法?希望这可以帮助您解决问题。