Skip to main content
Skip table of contents

Objects in columns

Examples in answer to questions such as:


columntextchunkimage

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.columntext;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;

import java.io.File;

public class ColumnTextChunkImage {
    public static final String DEST = "./target/sandbox/columntext/column_text_chunk_image.pdf";

    public static final String DOG = "./src/main/resources/img/dog.bmp";
    public static final String FOX = "./src/main/resources/img/fox.bmp";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ColumnTextChunkImage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Image dog = new Image(ImageDataFactory.create(DOG));
        Image fox = new Image(ImageDataFactory.create(FOX));
        Paragraph p = new Paragraph("quick brown fox jumps over the lazy dog.");
        p.add("Or, to say it in a more colorful way: quick brown ");
        p.add(fox);
        p.add(" jumps over the lazy ");
        p.add(dog);
        p.add(".");

        p.setWidth(350);
        doc.showTextAligned(p, 50, 650, TextAlignment.LEFT);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Columntext
{
    public class ColumnTextChunkImage
    {
        public static readonly String DEST = "results/sandbox/columntext/column_text_chunk_image.pdf";

        public static readonly String DOG = "../../../resources/img/dog.bmp";
        public static readonly String FOX = "../../../resources/img/fox.bmp";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new ColumnTextChunkImage().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Image dog = new Image(ImageDataFactory.Create(DOG));
            Image fox = new Image(ImageDataFactory.Create(FOX));
            Paragraph p = new Paragraph("quick brown fox jumps over the lazy dog.");
            p.Add("Or, to say it in a more colorful way: quick brown ");
            p.Add(fox);
            p.Add(" jumps over the lazy ");
            p.Add(dog);
            p.Add(".");

            p.SetWidth(350);
            doc.ShowTextAligned(p, 50, 650, TextAlignment.LEFT);

            doc.Close();
        }
    }
}


simplecolumn

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.columntext;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;

import java.io.File;

public class SimpleColumn {
    public static final String DEST = "./target/sandbox/columntext/simple_column.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new SimpleColumn().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(100, 120));

        Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text").setFontSize(4.5f);

        paragraph.setWidth(61);
        doc.showTextAligned(paragraph, 9,85, TextAlignment.LEFT);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Columntext
{
    public class SimpleColumn
    {
        public static readonly String DEST = "results/sandbox/columntext/simple_column.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new SimpleColumn().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, new PageSize(100, 120));

            Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text").SetFontSize(4.5f);

            paragraph.SetWidth(61);
            doc.ShowTextAligned(paragraph, 9, 85, TextAlignment.LEFT);

            doc.Close();
        }
    }
}


addlongtable

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class AddLongTable {
    public static final String DEST = "./target/sandbox/stamper/add_long_table.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddLongTable().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        Document doc = new Document(pdfDoc, PageSize.A4);
        doc.setTopMargin(72);
        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        for (int i = 0; i < 250; ) {
            table.addCell("Row " + (++i));
            table.addCell("Test");
        }
        doc.add(table);
        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddLongTable 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_long_table.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddLongTable().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document doc = new Document(pdfDoc, PageSize.A4);
            doc.SetTopMargin(72);
            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            for (int i = 0; i < 250; ) 
            {
                table.AddCell("Row " + (++i));
                table.AddCell("Test");
            }
            doc.Add(table);
            doc.Close();
        }
    }
}

columntextchunkimage

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.objects;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ColumnTextChunkImage {
    public static final String DOG = "src/main/resources/img/dog.bmp";
    public static final String FOX = "src/main/resources/img/fox.bmp";
    public static final String DEST = "./target/sandbox/objects/column_text_chunk_image.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ColumnTextChunkImage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        PdfImageXObject dog = new PdfImageXObject(ImageDataFactory.create(DOG));
        PdfImageXObject fox = new PdfImageXObject(ImageDataFactory.create(FOX));
        Paragraph p = new Paragraph("quick brown fox jumps over the lazy dog.")
                .add("Or, to say it in a more colorful way: quick brown ")
                .add(new Image(fox))
                .add(" jumps over the lazy ")
                .add(new Image(dog))
                .add(".")

                // The Leading is a spacing between lines of text
                .setMultipliedLeading(1);

        doc.add(p);

        doc.close();
    }

}

C#

C#
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Objects
{
    public class ColumnTextChunkImage
    {
        public static readonly string DOG = "../../../resources/img/dog.bmp";
        public static readonly string FOX = "../../../resources/img/fox.bmp";
        public static readonly string DEST = "results/sandbox/objects/column_text_chunk_image.pdf";

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new ColumnTextChunkImage().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            PdfImageXObject dog = new PdfImageXObject(ImageDataFactory.Create(DOG));
            PdfImageXObject fox = new PdfImageXObject(ImageDataFactory.Create(FOX));
            Paragraph p = new Paragraph("quick brown fox jumps over the lazy dog.")
                .Add("Or, to say it in a more colorful way: quick brown ")
                .Add(new Image(fox))
                .Add(" jumps over the lazy ")
                .Add(new Image(dog))
                .Add(".")
                
                // The Leading is a spacing between lines of text
                .SetMultipliedLeading(1);
            doc.Add(p);

            doc.Close();
        }
    }
}


columntextparagraphs

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.objects;

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.layout.LayoutArea;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.layout.RootLayoutArea;
import com.itextpdf.layout.renderer.DocumentRenderer;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ColumnTextParagraphs {
    public static final String DEST = "./target/sandbox/objects/column_text_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, 254, 770),
            new Rectangle(305, 36, 254, 770)
    };

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ColumnTextParagraphs().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        doc.setRenderer(new CustomDocumentRenderer(doc));

        int pCounter = 0;
        while (pCounter < 30) {
            doc.add(new Paragraph(String.format("Paragraph %s: %s", ++pCounter, TEXT)));
        }
        doc.close();
    }

    protected class CustomDocumentRenderer extends DocumentRenderer {
        int nextAreaNumber = 0;
        int currentPageNumber;

        public CustomDocumentRenderer(Document document) {
            super(document);
        }

        // If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
        // If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
        // renderer will be created
        @Override
        public IRenderer getNextRenderer() {
            return new DocumentRenderer(document);
        }

        @Override
        public LayoutArea updateCurrentArea(LayoutResult overflowResult) {
            if (nextAreaNumber % 2 == 0) {
                currentPageNumber = super.updateCurrentArea(overflowResult).getPageNumber();
            } else {
                new PdfCanvas(document.getPdfDocument(), document.getPdfDocument().getNumberOfPages())
                        .moveTo(297.5f, 36)
                        .lineTo(297.5f, 806)
                        .stroke();
            }
            currentArea = new RootLayoutArea(currentPageNumber, COLUMNS[nextAreaNumber % 2].clone());
            nextAreaNumber++;
            return currentArea;
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Layout;
using iText.Layout.Renderer;

namespace iText.Samples.Sandbox.Objects
{
    public class ColumnTextParagraphs
    {
        public static readonly string DEST = "results/sandbox/objects/column_text_paragraphs.pdf";
        public static readonly string TEXT = "This is some long paragraph " +
                                             "that will be added over and over again to prove a point.";
        public static readonly Rectangle[] COLUMNS =
        {
            new Rectangle(36, 36, 254, 770),
            new Rectangle(305, 36, 254, 770)
        };

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new ColumnTextParagraphs().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            doc.SetRenderer(new CustomDocumentRenderer(doc));

            int pCounter = 0;
            while (pCounter < 30)
            {
                doc.Add(new Paragraph(String.Format("Paragraph {0}: {1}", ++pCounter, TEXT)));
            }

            doc.Close();
        }

        protected class CustomDocumentRenderer : DocumentRenderer
        {
            int nextAreaNumber = 0;
            int currentPageNumber;

            public CustomDocumentRenderer(Document document) : base(document)
            {
            }

            // If renderer overflows on the next area iText will use default getNextRender() method with default renderer
            // parameters. So the method should be overridden with the parameters from the initial renderer
            public override IRenderer GetNextRenderer()
            {
                return new DocumentRenderer(document);
            }

            protected override LayoutArea UpdateCurrentArea(LayoutResult overflowResult)
            {
                if (nextAreaNumber % 2 == 0)
                {
                    currentPageNumber = base.UpdateCurrentArea(overflowResult).GetPageNumber();
                }
                else
                {
                    new PdfCanvas(document.GetPdfDocument(), document.GetPdfDocument().GetNumberOfPages())
                        .MoveTo(297.5f, 36)
                        .LineTo(297.5f, 806)
                        .Stroke();
                }

                currentArea = new RootLayoutArea(currentPageNumber, COLUMNS[nextAreaNumber % 2].Clone());
                nextAreaNumber++;
                return currentArea;
            }
        }
    }
}

listincolumn

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.objects;

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.properties.ListNumberingType;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ListInColumn {
    public static final String DEST = "./target/sandbox/objects/list_in_column.pdf";
    public static final String SRC = "./src/main/resources/pdfs/pages.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ListInColumn().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        while (pdfDoc.getNumberOfPages() > 2) {
            pdfDoc.removePage(pdfDoc.getLastPage());
        }
        Document doc = new Document(pdfDoc);
        doc.setRenderer(new ColumnDocumentRenderer(doc, new Rectangle[]{new Rectangle(250, 400, 250, 406)}));

        List list = new List(ListNumberingType.DECIMAL);
        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.");
        }
        doc.add(list);

        doc.close();
    }
}

C#

C#
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Objects
{
    public class ListInColumn
    {
        public static readonly string DEST = "results/sandbox/objects/list_in_column.pdf";
        public static readonly string SRC = "../../../resources/pdfs/pages.pdf";

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new ListInColumn().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            while (pdfDoc.GetNumberOfPages() > 2)
            {
                pdfDoc.RemovePage(pdfDoc.GetLastPage());
            }

            Document doc = new Document(pdfDoc);
            doc.SetRenderer(new ColumnDocumentRenderer(doc, new Rectangle[] {new Rectangle(250, 400, 250, 406)}));

            List list = new List(ListNumberingType.DECIMAL);
            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.");
            }

            doc.Add(list);

            doc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.