iText:在页面末尾拆分行

时间:2017-05-17 15:30:38

标签: java itext

如何防止分裂行? 在iText中有一些表格或单元格的方法我可以设置我不想要它吗?我需要保持所有行不可分割。 我没有将每一行作为一个新表,我在一个表中拥有所有数据。

我尝试了几件事,例如:table.setSplitRows(false); 但没有任何帮助我。

enter image description here 我的整个代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class Pdf5form {

public static final String DEST = "c:/radek-folder/pdf5form.pdf";
protected int horizontalAlignmentLeft = Element.ALIGN_LEFT;
protected int horizontalAlignmentCenter = Element.ALIGN_CENTER;
protected int verticalAlignmentMiddle = Element.ALIGN_MIDDLE;
protected int verticalAlignmentTop = Element.ALIGN_TOP;
protected String fontTypeRegular = "c:/radek-folder/font_sitebook.ttf";
protected String fontTypeBold = "c:/radek-folder/font_sitebook_bold.ttf";
protected float fontSizeRegular = 10f;
protected float fontSizeLarger = 14f;
protected float fontSizeLarge = 16f;
float tableWidth;
PdfPCell cell;

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

public void createPdf(String dest) throws IOException, DocumentException {
    float tiniestColumnWidth = 40;
    float tinyColumnWidth = 50;
    float smallColumnWidth = 55;
    float mediumColumnWidth = 120;
    float largeColumnWidth = 170; // celé číslo jinak háže čáry navíc
    float[] columns = { largeColumnWidth, tiniestColumnWidth, tinyColumnWidth,
            mediumColumnWidth, smallColumnWidth, smallColumnWidth, tinyColumnWidth,
            tinyColumnWidth, tinyColumnWidth, tinyColumnWidth, tinyColumnWidth,
            tinyColumnWidth };
    int numberOfColumns = columns.length;
    Document document = new Document(PageSize.A4.rotate(), 36, 36, 36, 64);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    writer.setPageEvent(new PageCounter());

    document.open();

    PdfPTable table = new PdfPTable(numberOfColumns);
    table.getDefaultCell().setPadding(0);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.setTotalWidth(columns); // 500
    table.setLockedWidth(true);
    cell = createAndReturnCell("Přenosové trasy GTS", horizontalAlignmentCenter,
            verticalAlignmentMiddle, numberOfColumns, 1, fontTypeBold, fontSizeLarge,
            BaseColor.BLACK);
    cell.setPaddingTop(10);
    cell.setPaddingBottom(15);
    table.addCell(cell);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Trasa [-]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Pásmo [GHz]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Kap a konf [Mbit/s]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Typ zařízení Index [-]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Zeměpis.délka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Zeměpis.šířka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Nadm.výška [m]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Výška ant.[m]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Průměr ant./zisk [m]/[dB]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Útlum vlnovod [dB]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Výkon vysílač dBm", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Délka trasy [m]", 1, 1, fontTypeBold, fontSizeRegular);

    for (int i = 0; i < 500; i++) {
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "CZ5142", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "38", 1, 3, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "1+0", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "FibeAir 1500p 38GHz", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "18?16'52''", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "49?49'52''", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "220", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "50", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0.30", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "10", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "1431.55", 1, 3, fontTypeRegular, fontSizeRegular);

        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "Ostrava Hornopolní 3308/40", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "100M", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "N/A", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "18?16'11''", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "49?50'30''", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "211", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "20", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0.30", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "10", 1, 2, fontTypeRegular, fontSizeRegular);

        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "CZ5305", 1, 1, fontTypeRegular, fontSizeRegular);
    }

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

private static void addCellToTableCzech(PdfPTable datatable, 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.BOX);
    datatable.addCell(cell);
}

private static PdfPCell createAndReturnCell(String value, int horizontalAlignment,
        int verticalAlignment, int colspan, int rowspan, String fontType,
        float fontSize, BaseColor color) {
    BaseFont base = null;
    try {
        base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Font font = new Font(base, fontSize);
    font.setColor(color);
    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);
    return cell;
}

class PageCounter extends PdfPageEventHelper {

    PdfTemplate t;
    Image total;

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        t = writer.getDirectContent().createTemplate(30, 16);
        try {
            total = Image.getInstance(t);
            total.setRole(PdfName.ARTIFACT);
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(3);
        try {
            table.setWidths(new int[] { 24, 24, 2 });
            table.setTotalWidth(tableWidth);

            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy / HH:mm");
            PdfPCell cell1 = new PdfPCell(new Phrase(dateFormat.format(new Date())));
            cell1.setVerticalAlignment(Element.ALIGN_TOP);
            cell1.setBorder(Rectangle.NO_BORDER);
            cell1.setFixedHeight(30);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(
                    new Phrase(String.format("Page %d of", writer.getPageNumber())));
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell2.setBorder(Rectangle.NO_BORDER);
            cell2.setFixedHeight(30);
            cell2.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(total);
            cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell3.setBorder(Rectangle.NO_BORDER);
            cell3.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell3);

            PdfContentByte canvas = writer.getDirectContent();
            canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
            table.writeSelectedRows(0, -1, 210, 40, canvas);
            canvas.endMarkedContentSequence();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
                new Phrase(String.valueOf(writer.getPageNumber())), 2, 2, 0);
    }
}
}

1 个答案:

答案 0 :(得分:1)

从我在评论中发布的链接, http://developers.itextpdf.com/question/how-prevent-splitting-table,您可以通过将表格嵌套到另一个表格中来保持表格结构的完整性:

PdfPTable nesting = new PdfPTable(1);
PdfPCell cell = new PdfPCell(table);
cell.setBorder(PdfPCell.NO_BORDER);
nesting.addCell(cell);
document.add(nesting);