Reputation:
I am creating a table using itext7.
I can change the color of a regular cell. How can I change the border to Null of a header cell?
I cannot add a check to create a "false" headerCell as the table may be a mutli-column so I would like the cells to be atop each column.
Having this false headerCell would just be on the first cell generated.
I tried:
//does not work.
table.setBorder(null);
//---------------------------does not work.
table.addHeaderCell("one: ").setBorder(Border.NO_BORDER);
table.addHeaderCell("two: ").setBorder(null).addStyle(normal);
table.addHeaderCell("three: ").setBorder(null).addStyle(normal);
Minimal:(just set your location) package application;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Text;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
exportRes();
}
public static void main(String[] args) {
launch(args);
}
public void exportRes() throws IOException{
PdfWriter writer = new PdfWriter(--location--);
PdfDocument pdf = new PdfDocument(writer);
pdf.getCatalog().setLang(new PdfString("en-US"));
pdf.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdf.getDocumentInfo();
info.setTitle("List");
Document document = new Document(pdf,PageSize.A4);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Style normal = new Style();
normal.setFont(font).setFontSize((float) 7.5);
Style notesF = new Style();
notesF.setFont(font).setItalic().setFontSize((float) 7.5);
Style lastNameF = new Style();
lastNameF.setFont(font).setFontSize((float) 7.5).setBold();
Table table = new Table(new float[]{(float) 2,29,10});
table.setWidthPercent(100);
table.setFont(font);
//does not work.
table.setBorder(null);
//---------------------------does not work.
table.addHeaderCell("one: ").setBorder(Border.NO_BORDER);
table.addHeaderCell("two: ").setBorder(null).addStyle(normal);
table.addHeaderCell("three: ").setBorder(null).addStyle(normal);
Cell check = new Cell().add(new Paragraph(" ").addStyle(normal).setFixedLeading(0)).setBorder(new SolidBorder(Color.LIGHT_GRAY, (float) 0.5));
table.addCell(check);
Text lastName = new Text("Name ").setBold().addStyle(lastNameF);
Text address = new Text("address").addStyle(normal);
Cell name = new Cell().add(new Paragraph().add(lastName).add(address)).setBorder(new SolidBorder(Color.LIGHT_GRAY, (float) 0.5));
table.addCell(name);
Cell notes = new Cell().add("notes").addStyle(notesF).setBorder(new SolidBorder(Color.LIGHT_GRAY, (float) 0.5));
table.addCell(notes);
document.add(table);
document.close();
}
}
Upvotes: 2
Views: 2019
Reputation: 2458
Your code is almost right. The problem is very simple and can be found in the next line:
table.addHeaderCell("one: ").setBorder(Border.NO_BORDER);
Notice that Table#addHeaderCell method returns Table not Cell object. So then you apply Border.NO_BORDER
on Table.
The solution is:
table.addHeaderCell(new Cell().add("one: ").setBorder(Border.NO_BORDER));
Also if you want to set specific border on header/footer (the analog of Table border), write summat table.getHeader().setBorder(your border)
. Notice also that itext applys table border on header/footer too.
I also want to mention that invoking Table constructor as you do isn't 100% correct if you use itext 7.0.2+. If you want columns widths to be in a ratio 2 29 30 (or any else), call Table(UnitValue.createPercentArray(new float[]{2, 29, 30)) ;
Upvotes: 3