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.
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();
}
}
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
Apparently (0, 0) is in the lower left in it text.
Also, it seems that it will be written from the specified position (100, 200) to the specified height 100 position.
In addition, Paragraph seems to be slightly out of position. It seems that you can specify this area more beautifully using Table.
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.
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