2017-06-20 191 views
0

我想要一个没有边框的表格。我尝试设置边框属性,单独的边框属性,手动设置边框,将单元格边框设置为无边框等。无删除边框。有没有边界的iText 7表的正确方法是什么?iText 7表忽略我的表格边框设置

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream)); 
    Document doc = new Document(pdfDoc); 
    PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA); 

    Table table = new Table(new float[] { 1, 1 }); 
    table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER); 
    table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER); 
    table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER); 
    table.setProperty(Property.BORDER_TOP, Border.NO_BORDER); 
    table.setProperty(Property.BORDER, Border.NO_BORDER); 
    table.setBorder(Border.NO_BORDER); 

    table.setWidthPercent(100); 

    // Header 
    File file = new ClassPathResource("logo.png").getFile(); 
    Image logo = new Image(ImageDataFactory.create(file.getPath())); 

    Paragraph headerParagraph = new Paragraph(); 
    Text headerTitle = new Text("Title of PDF") 
      .setFont(font) 
      .setFontSize(20) 
      .setFontColor(new DeviceRgb(0, 128, 128)); 
    Text headerDescription = new Text("Description") 
      .setFont(font) 
      .setFontSize(11); 

    headerParagraph.add(headerTitle); 
    headerParagraph.add(NEW_LINE); 
    headerParagraph.add(headerDescription); 

    table.addCell(logo); 
    table.addCell(headerParagraph).setTextAlignment(TextAlignment.RIGHT); 

这些设置都似乎work.Using的iText 7.0.2

+0

这是一个设计决定。如果我没有弄错,那么在[第5章]中解释(http://developers.itextpdf.com/content/itext-7-building-blocks/chapter-5-adding-abstractelement-objects-part-2)文档。 –

+0

据此,我的代码应该可以工作。 http://developers.itextpdf.com/content/best-itext-questions-stackoverview/tables/itext7-why-doesnt-getdefaultcellsetborderpdfpcellnoborder-have-any-effect –

回答

1

首先,运行下一个片段,看到iText7可以无国界创建表。

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName)); 
    Document doc = new Document(pdfDoc); 

    Table table = new Table(new float[] {50, 50 }); 

    Paragraph headerParagraph = new Paragraph(); 
    Text headerTitle = new Text("Title of PDF") 
      .setFontSize(20) 
      .setFontColor(new DeviceRgb(0, 128, 128)); 
    Text headerDescription = new Text("Description") 
      .setFontSize(11); 

    headerParagraph.add(headerTitle); 
    headerParagraph.add(headerDescription); 

    table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER)); 
    table.addCell(new Cell().add(headerParagraph).setBorder(Border.NO_BORDER).setTextAlignment(TextAlignment.RIGHT)); 

    doc.add(table); 

这是负责这种 “神奇” 的路线:

table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER)); 

但是没有魔法可言。 默认情况下,单元格在iText7中具有边框(0.5px纯黑色)。所以如果你想添加一个没有边框的单元格,你应该通过将NO_BORDER设置为单元格边框来指定它。

另一方面,表格默认没有边框(我的意思是边框)。所以在这些线路没有必要:

table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER); 
    table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER); 
    table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER); 
    table.setProperty(Property.BORDER_TOP, Border.NO_BORDER); 
    table.setProperty(Property.BORDER, Border.NO_BORDER); 
    table.setBorder(Border.NO_BORDER); 

你也应该明白,table.setBorder(border)代表table.setProperty(Property.BORDER, border)table.setBorderLeft(border)等相同

+0

谢谢你,配置每个单元格允许我的表没有边界。也就是说,该文档指出,如果您在表格上设置属性,它将设置您在调用add()时生成的单元格。 –

+0

这部分文档确实是错误的。我会要求我们的人员修复它。 –