Create PDF with itext7-Free layout: Text-

Let's arrange the text freely

In the output up to the last time, it was only exported from the upper left like Word. In PDF, you can specify the output position and output.

Code immediately

FreePosString.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.Paragraph;

public class FreePosString {
    public static void main(String[] args) throws Exception {
        PdfDocument pdf = new PdfDocument(new PdfWriter("FreePosString.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, 50, 100)
        };
        document.setRenderer(new ColumnDocumentRenderer(document, columns));

        //Red frame-Line width 0.Write characters with 5f
        Border border = new SolidBorder(ColorConstants.RED, 0.5f);
        Paragraph p1 = new Paragraph("Aiue, Kakikukeko abcdefgh-ijklmn,opqrstuvwxyz")
                .setFont(font)
                .setFontSize(12)
                .setBorder(border)
                ;
        document.add(p1);
        
        // (100, 200)Line to. width:50, height:Check the range of 100
        canvas.moveTo(0, 0).lineTo(100, 200).lineTo(150, 200).lineTo(150, 300).lineTo(100, 300).stroke();

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

Description

document.setRenderer(new ColumnDocumentRenderer(document, columns)); Then, specify multiple Rectangles in columns. For the sake of simplicity this time, specify only one position (100,200), width: 50 and height: 100. Also, to make the location easier to understand, try drawing a line in the specified range using moveTo and lineTo.

Output image FreePosString.jpg

(0, 0) is the lower left.

Apparently (0, 0) is in the lower left in it text.

Export is specified position + specified height.

Also, it seems that it will be written from the specified position (100, 200) to the specified height 100 position.

Is Paragraph off?

In addition, Paragraph seems to be slightly out of position. It seems that you can specify this area more beautifully using Table.

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

Characters that did not fit in the specified frame were output at the same position on the next and subsequent pages. I would also like to find a way to determine if it is out of the way.

Is kinsoku processing only half-width alphanumeric characters?

Also, I adjusted the character string so that ",", "-", and "," are added to the beginning and end of the line, but it seems that only half-width alphanumeric characters are prohibited. This setting was possible in the previous version, but is it possible in it text7? I would like to investigate this area in the future.

Recommended Posts

Create PDF with itext7-Free layout: Text-
Create PDF with itext7-Free layout: Table-
Create PDF with itext7 ~ Hello World ~
Sample to create PDF from Excel with Ruby
Text extraction in Java from PDF with pdfbox-2.0.8
Try create with Trailblazer
Create XML-RPC API with Wicket
Paging PDF with Java + PDFBox.jar
Create a playground with Xcode 12
Create microservices with Spring Boot