Skip to main content
Skip table of contents

Links in tables

Example that adds a link into a table that is added using writeSelectedRows().


linkinpositionedtable

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.tables;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class LinkInPositionedTable {
    public static final String DEST = "./target/sandbox/tables/link_in_positioned_table.pdf";

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

        new LinkInPositionedTable().manipulatePdf(DEST);
    }

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

        Table table = new Table(UnitValue.createPercentArray(1));
        table.setWidth(500);

        Cell cell = new Cell();
        Paragraph p = new Paragraph();

        Link link = new Link("link to top of next page", PdfAction.createGoTo("top"));
        p.add(link);
        cell.add(p);
        table.addCell(cell);

        doc.add(table);
        doc.add(new AreaBreak());

        // Creates a target that the link leads to
        Paragraph target = new Paragraph("top");
        target.setDestination("top");
        doc.add(target);

        doc.close();
    }
}

C#

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

namespace iText.Samples.Sandbox.Tables
{
    public class LinkInPositionedTable
    {
        public static readonly string DEST = "results/sandbox/tables/link_in_positioned_table.pdf";

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

            new LinkInPositionedTable().ManipulatePdf(DEST);
        }

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

            Table table = new Table(UnitValue.CreatePercentArray(1));
            table.SetWidth(500);

            Cell cell = new Cell();
            Paragraph p = new Paragraph();

            Link link = new Link("link to top of next page", PdfAction.CreateGoTo("top"));
            p.Add(link);
            cell.Add(p);
            table.AddCell(cell);

            doc.Add(table);
            doc.Add(new AreaBreak());

            // Creates a target that the link leads to
            Paragraph target = new Paragraph("top");
            target.SetDestination("top");
            doc.Add(target);

            doc.Close();
        }
    }
}


JavaScript errors detected

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

If this problem persists, please contact our support.