package it.spaghettisource.certgen.services.pdf; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Random; import org.apache.commons.lang3.RandomStringUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Font.FontFamily; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.draw.LineSeparator; public class ColumnTextSeparatorIText { private String filePath; @Before public void beforeTest() { filePath = System.getProperty("user.dir")+"/src/test/resources"; } @After public void afterTest() { } public static final float[][] COLUMNS = {{ 36, 36, 296, 806 } , { 299, 36, 559, 806 }}; @Test public void test_OK_columnTextSeparatorIText() throws Exception{ File RESULT = new File(filePath+"/test_OK_columnTextSeparatorIText.pdf"); // step 1 Document document = new Document(PageSize.A4); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter pdfWriter = PdfWriter.getInstance(document, baos); document.open(); PdfContentByte writed = pdfWriter.getDirectContent(); ColumnText ct = new ColumnText(writed); Phrase p = null; for (int i = 0; i < 50; i++) { p = new Phrase(); p.add(new LineSeparator(0.3f, 100, null, Element.ALIGN_CENTER, -2)); ct.addText(p); ct.addText(Chunk.NEWLINE); } ct.setAlignment(Element.ALIGN_JUSTIFIED); ct.setExtraParagraphSpace(6); ct.setLeading(0, 1.2f); ct.setFollowingIndent(27); int linesWritten = 0; int column = 0; int status = ColumnText.START_COLUMN; while (ColumnText.hasMoreText(status)) { ct.setSimpleColumn(COLUMNS[column][0], COLUMNS[column][1],COLUMNS[column][2], COLUMNS[column][3]); ct.setYLine(COLUMNS[column][3]); status = ct.go(); linesWritten += ct.getLinesWritten(); column = Math.abs(column - 1); if (column == 0) document.newPage(); } ct.go(); ct.addText(new Phrase("Lines written: " + linesWritten)); document.close(); FileOutputStream fos = new FileOutputStream(RESULT); fos.write(baos.toByteArray()); fos.close(); } }