Java 更改行间距
我已经尝试了几个小时来处理iText中的一个乍一看非常简单的问题,看看这张描述我问题的图片: 请有人修改一下我的代码,让它以我想要的方式开始输出 这是我的密码:Java 更改行间距,java,itext,Java,Itext,我已经尝试了几个小时来处理iText中的一个乍一看非常简单的问题,看看这张描述我问题的图片: 请有人修改一下我的代码,让它以我想要的方式开始输出 这是我的密码: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import
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中的错误,而是代码中的错误。我没有时间,也没有意愿测试你的代码。根据我看到的情况,我用手机接听了电话。如果你想让某人更仔细地看一看,总是有付费支持的。布鲁诺在他的回答中正确地分析了这个问题:在第一个单元格之后添加第二个单元格,其中的行跨度为2,这是没有意义的,因为很明显,它是在第二列中绘制的,而不是在第十二列中绘制的 然而,他离线创建的解决方案是不正确的 如果希望第二个单元格(第2行)出现在第十二列中,则必须将其添加为第十二个单元格!假设所有涉及的单元格都有第1列,即。。。就像手头的案子一样 因此:
testUseRowspanLikeUser7968180Fixed使用的方法CreatePdfixed我没有正确理解iText表是如何创建的。这导致我无法正确计算:
当我意识到那幅画里是什么的时候,我已经很顺利地完成了。对不起,这只是我的错。这看起来像是一个与@BrunoLowagie相同的问题-我不会把这个问题理解为责备别人。大多数人,也许包括你在内,都在一些琐碎的事情上花费了过多的时间。只要接受这个问题并提供一个有用的答案就行了。iText是开源的。如果你认为有一个bug,请随意修复它。所以我想我是真的,你甚至没有尝试编译你的解决方案,现在我是一个坏的,因为我告诉过你它不起作用,而且在我发布这个问题之前我已经试过了。好吧,那么,我的老板会很高兴的。我不能在手机上编译代码。我阅读了您的代码,发现了一个明显的逻辑错误,并解释了错误。如果你想让我编译和测试代码,我需要腾出时间,我的时间不是免费的。你可以向老板解释,获得他使用iText的许可证也使你有权获得支持,从而让老板高兴。如果你制作了一张支持票来解释你的问题,你会得到一个答案,并且避免了当你想免费得到某样东西时所带来的挫折感。另外:你选择使用iText 5而不是iText 7有点奇怪。iText 5处于维护模式,这意味着它将在2018年底结束使用,对iText 5的支持将在2019年停止。我根据您的示例进行了计算。我计算了您添加的单元格,考虑了每个单元格的colspan和rowspan。那个数学是错的。而且,为什么我要为那些叫我花花公子的人做一件事呢?谢谢,但我已经把它修好了,所以我不会再检查了:-但是谢谢你的努力。
// 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);
public void createPdfFixed(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);
// cell 1 with rowspan 2 >> column 1
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "Brno Špitálka 8 Brno Hájecká 1068/14 CZ5159", 1,
2, fontTypeRegular, fontSizeRegular);
// cells 2-11 with rowspan 1 >> column 2-11 upper row
for (int i = 2; i < 12; i++) {
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular,
fontSizeRegular);
}
// cell 12 with rowspan 2 >> column 12
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 2, fontTypeRegular, fontSizeRegular);
// cells 13-22 with rowspan 1 >> column 2-11 lower row
for (int i = 13; i < 23; i++) {
addCellToTableCzech(subTableZkouska, horizontalAlignmentCenter,
verticalAlignmentMiddle, "38", 1, 1, fontTypeRegular,
fontSizeRegular);
}
document.add(subTableZkouska);
document.close();
}