如何在itext中拆分单元格?

时间:2014-07-04 09:57:21

标签: java itext

我需要使用iTextPDF将细胞分成两部分。如下图所示。

这是我需要分成两个单元格的Planned单元格

PdfPTable row4 = new PdfPTable(3);
row4.setWidthPercentage(100);
row4.setSpacingBefore(10);
row4.setSpacingAfter(10);

PdfPCell monday = new PdfPCell(new Paragraph("Monday ", tableheadres));
monday.setMinimumHeight(15f);
monday.setLeading(0f, 1.2f);

PdfPCell mondayvalue = new PdfPCell(new Paragraph("social social,gfdgdf,fdfgfdg,fdgdfgfdsfsdfsd,ggdshgfgskfs,sggfsdfskjfjgshfjsgaf,shgjkfjk", tableval));
mondayvalue.setMinimumHeight(15f);
mondayvalue.setLeading(0f, 1.2f);

PdfPCell Planned = new PdfPCell(new Paragraph("Planned", tableheadres));
Planned.setMinimumHeight(15f);
Planned.setLeading(0f, 1.2f);

PdfPCell plansubstitue = new PdfPCell(new Paragraph("social social,gfdgdf,fdfgfdg,fdgdfgfdsfsdfsd,ggdshgfgskfs,sggfsdfskjfjgshfjsgaf,shgjkfjk", tableval));

row4.addCell(monday);
row4.addCell(mondayvalue);

row4.addCell(Planned);
row4.addCell(plansubstitue);

document.add(row4);

嵌套代码:

PdfPCell activity = new PdfPCell(new Paragraph("Activities / Approach / Introduction", tableheadres));
activity.setMinimumHeight(15f);
activity.setLeading(0f, 1.2f);
// activity.setRowspan(2);

PdfPTable activitynested = new PdfPTable(1);
PdfPCell activitynestedcell = new PdfPCell(new Phrase("Nested Cell 1"));
activitynestedcell.setHorizontalAlignment(Element.ALIGN_CENTER);
activity.addElement(activitynested);

1 个答案:

答案 0 :(得分:0)

实际上,转储代码并不是一个好习惯,这就是你想要的例子:

public static void main(String arr[]) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("TableNestedDemo.pdf"));
            document.open();
            PdfPTable row4 = new PdfPTable(4);
            row4.setWidthPercentage(100);
            row4.setSpacingBefore(10);
            row4.setSpacingAfter(10);

            PdfPCell monday = new PdfPCell(new Paragraph("Monday "));
            monday.setMinimumHeight(15f);
            monday.setLeading(0f, 1.2f);

            PdfPCell mondayvalue = new PdfPCell(new Paragraph("social social,gfdgdf,fdfgfdg,fdgdfgfdsfsdfsd,ggdshgfgskfs,sggfsdfskjfjgshfjsgaf,shgjkfjk"));
            mondayvalue.setMinimumHeight(15f);
            mondayvalue.setLeading(0f, 1.2f);

            PdfPCell Planned = new PdfPCell(new Paragraph("Planned"));
            Planned.setMinimumHeight(15f);
            Planned.setLeading(0f, 1.2f);

            PdfPCell plansubstitue = new PdfPCell(new Paragraph("social social,gfdgdf,fdfgfdg,fdgdfgfdsfsdfsd,ggdshgfgskfs,sggfsdfskjfjgshfjsgaf,shgjkfjk"));

            PdfPCell activity = new PdfPCell(new Paragraph("Activities / Approach / Introduction"));
            activity.setColspan(4);
            activity.setMinimumHeight(15f);
            activity.setLeading(0f, 1.2f);

            PdfPTable activitynested = new PdfPTable(1);
            PdfPCell activitynestedcell = new PdfPCell(new Phrase("Nested Cell 1"));
            activitynestedcell.setHorizontalAlignment(Element.ALIGN_CENTER);
            activitynested.addCell(activitynestedcell);
            activity.addElement(activitynested);
            row4.addCell(monday);
            row4.addCell(mondayvalue);

            row4.addCell(Planned);
            row4.addCell(plansubstitue);
            row4.addCell(activity);

            document.add(row4);
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }