[JAVA] Create PDF with itext7-Free layout: Table-

Let's arrange the table freely

This time, the table is freely arranged. In the previous Paragraph, the position was slightly off, so I think it is one way to arrange the character strings in a table with one cell.

Code immediately

FreePosTable.java


import com.itextpdf.kernel.colors.ColorConstants;
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.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;

public class FreePosTable {
    public static void main(String[] args) throws Exception {
        PdfDocument pdf = new PdfDocument(new PdfWriter("FreePosTable.pdf"));
        PageSize ps = PageSize.A4.rotate();
        PdfPage page = pdf.addNewPage(ps);
        PdfCanvas canvas = new PdfCanvas(page);

        
        //Heisei Kaku Gothic
        PdfFont font = PdfFontFactory.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H");
 

        // Initialize document
        Document document = new Document(pdf);
 
        // (100, 200)To width:50, height:Specify 100
        Rectangle[] columns = {
                new Rectangle(100, 200, 400, 100), // x,y,width,height
        };
        document.setRenderer(new ColumnDocumentRenderer(document, columns));

        //Table definition: 3 columns with a width ratio of 100:200:Designated as 300
        Table table = new Table(new float[]{100,200,300});
        table.setWidth(UnitValue.createPercentValue(100))
                .setTextAlignment(TextAlignment.CENTER)
                .setHorizontalAlignment(HorizontalAlignment.CENTER);

        //Table header specification
        Cell cell = new Cell();
        cell.add(new Paragraph("Header 1"));
        table.addCell(cell);
        Cell cell2 = new Cell();
        cell2.add(new Paragraph("Header 2"));
        table.addCell(cell2);
        Cell cell3 = new Cell();
        cell3.add(new Paragraph("Header 3"));
        table.addCell(cell3);

        //Specify 3 rows of table body
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                table.addCell("cell" + x + "-" + y);
            }
        }

        //Table footer specification
        table.addFooterCell("Footer 1");
        table.addFooterCell("Footer 2");
        table.addFooterCell("Footer 3");

        //Specify the frame of the table, etc.
        Border border = new SolidBorder(ColorConstants.RED, 1.5f);
        table.setFont(font);
        table.setBorder(border);
        document.add(table);

        // (100, 200)Line to. width:400, height:Check the range of 100
        //Also, put a line every 100 at the top
        canvas.moveTo(0, 0).lineTo(100, 200).lineTo(500, 200)
        .lineTo(500, 310).moveTo(500, 300)
        .lineTo(400, 300).lineTo(400, 310).moveTo(400, 300)
        .lineTo(300, 300).lineTo(300, 310).moveTo(300, 300)
        .lineTo(200, 300).lineTo(200, 310).moveTo(200, 300)
        .lineTo(100, 300).lineTo(100, 310).moveTo(100, 300)
        .stroke();

        //Close document
        document.close();
    }
}

Description

The position specification is the same as last time. In order to make the width easy to understand, I made a step in every 100.

Output image FreePosTable.jpg

Export is specified position + specified height.

The table is also displayed from the height above the specified position in the same way as the character string. There seems to be no gap in the table.

The excess is output at the same position on the next page.

The lines that also stick out are displayed on the next page. However, the header footer seems to be displayed on each page.

Specify column width

Table table = new Table (new float [] {100,200,300}); indicates that 3 columns are specified and the ratio of each width is 100: 200: 300. The specified width is 400, but the width is set as a percentage regardless of this. However, for some reason, the new float [] {1,2,3} has the same width (it seems that the old version was made?), And the new float [] {10,20,30} causes an error. It is had. why. For the time being, it seems that it should be specified with a somewhat large value. ,

Recommended Posts

Create PDF with itext7-Free layout: Table-
Create PDF with itext7-Free layout: Text-
Create PDF with itext7 ~ Hello World ~
Sample to create PDF from Excel with Ruby
Try create with Trailblazer
Java adds table to PDF
Paging PDF with Java + PDFBox.jar
Output multiplication table with Stream
Create a playground with Xcode 12
Create table and add columns
Create microservices with Spring Boot