gpt4 book ai didi

java - 更改行跨度

转载 作者:行者123 更新时间:2023-12-02 13:09:52 24 4
gpt4 key购买 nike

我已经尝试了几个小时在 iText 中处理(乍一看)非常简单的问题,看看这张描述我的问题的图片:

enter image description here有人可以改变我的代码开始发出我想要的输出的方式吗?

这是我的代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class App {
public static final String DEST = "c:/radek-folder/pdf1iTextZKOUSKA.pdf";
protected int horizontalAlignmentCenter = Element.ALIGN_CENTER;
protected int verticalAlignmentMiddle = Element.ALIGN_MIDDLE;
protected String fontTypeRegular = "c:/radek-folder/font_sitebook.ttf";
protected float fontSizeRegular = 10f;

public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new App().createPdf(DEST);
System.out.println("done");
}

public void createPdf(String dest) throws IOException, DocumentException {
float[] columns = { 100, 50, 100, 50, 50, 50, 50, 50, 75, 50, 50, 50 };
int numberOfColumns = columns.length;
Document document = new Document(PageSize.A4.rotate(), 36, 36, 36, 36);
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();

PdfPTable subTableZkouska = new PdfPTable(numberOfColumns);
subTableZkouska.setTotalWidth(columns);
subTableZkouska.setLockedWidth(true);

addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "Brno Špitálka 8 Brno Hájecká 1068/14 CZ5159", 1,
2, fontTypeRegular, fontSizeRegular);

addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 2, fontTypeRegular, fontSizeRegular);

for (int i = 0; i < 19; i++) {
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular,
fontSizeRegular);
}
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular, fontSizeRegular);

document.add(subTableZkouska);
document.close();
}

private static void addCellToTableCzech(PdfPTable table, int horizontalAlignment,
int verticalAlignment, String value, int colspan, int rowspan,
String fontType, float fontSize) {
BaseFont base = null;
try {
base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(base, fontSize);
PdfPCell cell = new PdfPCell(new Phrase(value, font));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(verticalAlignment);
cell.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell);
}
}

最佳答案

您正在创建一个包含 12 列的表格,然后添加如下单元格:

// column 1, rows 1 and 3
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "Brno Špitálka 8 Brno Hájecká 1068/14 CZ5159", 1,
2, fontTypeRegular, fontSizeRegular);
// column 2, rows 1 and 2
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 2, fontTypeRegular, fontSizeRegular);
// columns 3 to 12, rows 1 and 2
for (int i = 0; i < 19; i++) {
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular,
fontSizeRegular);
}
// column 1, row 3
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular, fontSizeRegular);

如文档所述,iText 的 PdfPTable 仅呈现完整的行,因此最后一行将被忽略。这与您分享的有关使用 iText 创建的 PDF 的屏幕截图一致。您为什么声称 iText 存在错误?

看看所需的输出,我认为您应该有以下代码:

// column 1, rows 1 and 2
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "Brno Špitálka 8 Brno Hájecká 1068/14 CZ5159", 1,
2, fontTypeRegular, fontSizeRegular);
// columns 2 to 11, rows 1 and 2
for (int i = 0; i < 19; i++) {
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular,
fontSizeRegular);
}
// column 12, rows 1 and 2
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 2, fontTypeRegular, fontSizeRegular);

结论:我不认为这是 iText 中的错误,而是您代码中的错误。我没有时间,也没有意愿测试你的代码。我根据我所看到的情况通过手机进行了回答。如果您希望有人仔细观察,总有付费支持。

关于java - 更改行跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44005834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com