Objects in columns
Examples in answer to questions such as:
columntextparagraphs
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in
*/
package sandbox.objects;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class ColumnTextParagraphs {
public static final String DEST = "results/objects/column_paragraphs.pdf";
public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point.";
public static final Rectangle[] COLUMNS = {
new Rectangle(36, 36, 290, 806),
new Rectangle(305, 36, 559, 806)
};
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ColumnTextParagraphs().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
int side_of_the_page = 0;
ct.setSimpleColumn(COLUMNS[side_of_the_page]);
int paragraphs = 0;
while (paragraphs < 30) {
ct.addElement(new Paragraph(String.format("Paragraph %s: %s", ++paragraphs, TEXT)));
while (ColumnText.hasMoreText(ct.go())) {
if (side_of_the_page == 0) {
side_of_the_page = 1;
canvas.moveTo(297.5f, 36);
canvas.lineTo(297.5f, 806);
canvas.stroke();
}
else {
side_of_the_page = 0;
document.newPage();
}
ct.setSimpleColumn(COLUMNS[side_of_the_page]);
}
}
// step 5
document.close();
}
}
columntextparagraphs2
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/29378407/how-can-you-eliminate-white-space-in-multiple-columns-using-itextsharp
*/
package sandbox.objects;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class ColumnTextParagraphs2 {
public static final String DEST = "results/objects/column_paragraphs2.pdf";
public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point.";
public static final float COLUMN_WIDTH = 254;
public static final float ERROR_MARGIN = 16;
public static final Rectangle[] COLUMNS = {
new Rectangle(36, 36, 36 + COLUMN_WIDTH, 806),
new Rectangle(305, 36, 305 + COLUMN_WIDTH, 806)
};
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ColumnTextParagraphs2().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
addContent(ct);
float height = getNecessaryHeight(ct);
addContent(ct);
Rectangle left;
float top = COLUMNS[0].getTop();
float middle = (COLUMNS[0].getLeft() + COLUMNS[1].getRight()) / 2;
float columnheight;
int status = ColumnText.START_COLUMN;
while (ColumnText.hasMoreText(status)) {
if (checkHeight(height)) {
columnheight = COLUMNS[0].getHeight();
left = COLUMNS[0];
}
else {
columnheight = (height / 2) + ERROR_MARGIN;
left = new Rectangle(
COLUMNS[0].getLeft(),
COLUMNS[0].getTop() - columnheight,
COLUMNS[0].getRight(),
COLUMNS[0].getTop()
);
}
// left half
ct.setSimpleColumn(left);
ct.go();
height -= COLUMNS[0].getTop() - ct.getYLine();
// separator
canvas.moveTo(middle, top - columnheight);
canvas.lineTo(middle, top);
canvas.stroke();
// right half
ct.setSimpleColumn(COLUMNS[1]);
status = ct.go();
height -= COLUMNS[1].getTop() - ct.getYLine();
// new page
document.newPage();
}
// step 5
document.close();
}
public void addContent(ColumnText ct) {
for (int i = 0; i < 35; i++) {
ct.addElement(new Paragraph(String.format("Paragraph %s: %s", i, TEXT)));
}
}
public float getNecessaryHeight(ColumnText ct) throws DocumentException {
ct.setSimpleColumn(new Rectangle(0, 0, COLUMN_WIDTH, -500000));
ct.go(true);
return -ct.getYLine();
}
public boolean checkHeight(float height) {
height -= COLUMNS[0].getHeight() + COLUMNS[1].getHeight() + ERROR_MARGIN;
return height > 0;
}
}
listincolumn
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/29277611/itextsharp-continuing-ordered-list-on-second-page-with-a-number-other-than-1
*/
package sandbox.objects;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class ListInColumn {
public static final String SRC = "resources/pdfs/pages.pdf";
public static final String DEST = "results/objects/list_in_column.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ListInColumn().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
reader.selectPages("1-2");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
List list = new List(List.ORDERED);
for (int i = 0; i < 10; i++) {
list.add("This is a list item. It will be repeated a number of times. "
+ "This is done only for test purposes. "
+ "I want a list item that is distributed over several lines.");
}
ColumnText ct = new ColumnText(stamper.getOverContent(1));
ct.addElement(list);
Rectangle rect = new Rectangle(250, 400, 500, 806);
ct.setSimpleColumn(rect);
int status = ct.go();
if (ColumnText.hasMoreText(status)) {
ct.setCanvas(stamper.getOverContent(2));
ct.setSimpleColumn(rect);
ct.go();
}
stamper.close();
}
}
columntextchunkimage
JAVA
JAVA
/*
* This example was written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/30843163/how-to-add-inline-images-in-itext-when-using-columntext-in-text-mode
*/
package sandbox.columntext;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
*
* @author Bruno Lowagie (iText Software)
*/
@WrapToTest
public class ColumnTextChunkImage {
public static final String DOG = "resources/images/dog.bmp";
public static final String FOX = "resources/images/fox.bmp";
public static final String DEST = "results/columntext/column_text_chunk_image.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ColumnTextChunkImage().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
Image dog = Image.getInstance(DOG);
Image fox = Image.getInstance(FOX);
Phrase p = new Phrase("quick brown fox jumps over the lazy dog.");
p.add("Or, to say it in a more colorful way: quick brown ");
p.add(new Chunk(fox, 0, 0, true));
p.add(" jumps over the lazy ");
p.add(new Chunk(dog, 0, 0, true));
p.add(".");
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(new Rectangle(50, 600, 400, 800));
ct.addText(p);
ct.go();
// step 5
document.close();
}
}
tableincolumn
JAVA
JAVA
package sandbox.objects;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.log.LoggerFactory;
import com.itextpdf.text.log.SysoLogger;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class TableInColumn {
public static final String DEST = "results/objects/table_in_column.pdf";
private int status = ColumnText.START_COLUMN;
private float y_position = 0;
public static void main(String[] args) throws IOException, DocumentException {
LoggerFactory.getInstance().setLogger(new SysoLogger());
File file = new File(DEST);
file.getParentFile().mkdirs();
new TableInColumn().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte pagecanvas = writer.getDirectContent();
PdfPTable table;
PdfPCell cell = new PdfPCell();
for (int i = 1; i <= 30; i++)
cell.addElement(new Paragraph("Line " + i));
table = new PdfPTable(1);
table.setSplitLate(false);
table.addCell(cell);
addTable(document, pagecanvas, table);
document.newPage();
table = new PdfPTable(1);
table.setSplitLate(false);
table.addCell(cell);
table.addCell(cell);
addTable(document, pagecanvas, table);
// step 5
document.close();
}
public void addTable(Document document, PdfContentByte canvas, PdfPTable table) throws DocumentException {
Rectangle pagedimension = new Rectangle(36, 36, 559, 806);
System.out.println("Adding table to try");
drawColumnText(document, canvas, pagedimension, table, true);
Rectangle rect;
if (ColumnText.hasMoreText(status)) {
rect = pagedimension;
}
else {
rect = new Rectangle(36, 36, 559, 806 - ((y_position - 36) / 2));
}
System.out.println("Adding table for real");
drawColumnText(document, canvas, rect, table, false);
}
public void drawColumnText(Document document, PdfContentByte canvas, Rectangle rect, PdfPTable table, boolean simulate) throws DocumentException {
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(rect);
ct.addElement(table);
status = ct.go(simulate);
y_position = ct.getYLine();
System.out.println("First go: " + status);
while (!simulate && ColumnText.hasMoreText(status)) {
document.newPage();
ct.setSimpleColumn(rect);
status = ct.go(simulate);
System.out.println("Next go? " + status);
}
System.out.println("Final status: " + status);
}
}
addlongtable
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/21838846/inserting-a-table-into-multiple-pdf-pages-with-columntext
*
* The person asking the question forgot to set the canvas for the ColumnText object.
*/
package sandbox.stamper;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class AddLongTable {
public static final String SRC = "resources/pdfs/hello.pdf";
public static final String DEST = "results/stamper/hello_table.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddLongTable().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfPTable table = new PdfPTable(2);
for (int i = 0; i < 250; ) {
table.addCell("Row " + (++i));
table.addCell("Test");
}
PdfReader reader = new PdfReader(src);
int pageNum = 1;
Rectangle page = reader.getPageSize(pageNum);
Rectangle rect = new Rectangle(36, 36, page.getRight(36), page.getTop(72));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
ColumnText ct = new ColumnText(stamper.getOverContent(pageNum));
ct.addElement(table);
while(true) {
ct.setSimpleColumn(rect);
if (!ColumnText.hasMoreText(ct.go()))
break;
stamper.insertPage(++pageNum, page);
ct.setCanvas(stamper.getOverContent(pageNum));
}
stamper.close();
reader.close();
}
}
simplecolumn
JAVA
JAVA
/*
* This example was written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/34445641
*/
package sandbox.columntext;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
* @author Bruno Lowagie (iText Software)
*/
@WrapToTest
public class SimpleColumn {
public static final String DEST = "results/columntext/simple_column.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SimpleColumn().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Rectangle rect = new Rectangle(100, 120);
Document document = new Document(rect);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
BaseFont baseFont = BaseFont.createFont();
ColumnText ct = new ColumnText(canvas);
Font paragraphFont=new Font(baseFont,4.5f);
ct.setSimpleColumn(9, 70, 70, 95);
Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text",paragraphFont);
ct.addElement(paragraph);
ct.go();
// step 5
document.close();
}
}