改变rowpans

时间:2017-05-16 15:37:32

标签: java itext

我已经尝试了几个小时处理(一见钟情)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);
}
}

3 个答案:

答案 0 :(得分:1)

您正在创建一个包含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中的错误,但是代码中存在错误。我没有时间,也没有测试代码的倾向。我根据我的看法从手机上回答。如果你想让别人仔细看看,总会得到支持。

答案 1 :(得分:0)

布鲁诺在他的回答中正确地分析了这个问题:在第一个单元后面添加第二个单元格没有任何意义,因为它显然是在第二列中绘制的,而不是在第二列中绘制的OP。

他离线创建的解决方案不正确。

如果有人希望第二列中有第二个单元格出现在第十二列中,则必须将其添加为第十二个单元格! (假设所涉及的所有单元都具有columnpan 1,那就是......就像在手边的情况一样。)

因此:

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();
}

(测试createPdfFixed使用的UseRowspan方法testUseRowspanLikeUser7968180Fixed

答案 2 :(得分:0)

我没有正确理解iText表的创建方式。这导致我无法正确计算它:

enter image description here 当我意识到那张照片中的内容时,我已经很顺利地管理了它。抱歉,这只是我的错。