Skip to main content
Skip table of contents

Adding links in a table cell


linkintablecell

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.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.layout.Document;
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 com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new LinkInTableCell().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)).useAllAvailableWidth();

        // Part of the content is a link:
        Paragraph paragraph = new Paragraph();
        paragraph.add("iText at the ");
        Link chunk = new Link("European Business Awards",
                PdfAction.createURI("https://itextpdf.com/en/events/itext-european-business-awards-gala-milan"));
        paragraph.add(chunk);
        paragraph.add(" gala in Milan");
        table.addCell(paragraph);

        // The complete cell is a link:
        Cell cell = new Cell().add(new Paragraph("Help us win a European Business Award!"));
        cell.setNextRenderer(new LinkInCellRenderer(cell, "http://itextpdf.com/blog/help-us-win-european-business-award"));
        table.addCell(cell);

        // The complete cell is a link (using SetAction() directly on cell):
        cell = new Cell().add(new Paragraph(
                "IText becomes Belgium’s National Public Champion in the 2016 European Business Awards"));
        cell.setAction(PdfAction.createURI(
                "http://itextpdf.com/en/blog/itext-becomes-belgiums-national-public-champion-2016-european-business-awards"));
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }


    private static class LinkInCellRenderer extends CellRenderer {
        protected String url;

        public LinkInCellRenderer(Cell modelElement, String url) {
            super(modelElement);
            this.url = url;
        }

        // 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 LinkInCellRenderer((Cell) modelElement, url);
        }

        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);

            PdfLinkAnnotation linkAnnotation = new PdfLinkAnnotation(getOccupiedAreaBBox());
            linkAnnotation.setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
            linkAnnotation.setAction(PdfAction.createURI(url));
            drawContext.getDocument().getLastPage().addAnnotation(linkAnnotation);
        }
    }
}

C#

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

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

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

            new LinkInTableCell().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)).UseAllAvailableWidth();

            // Part of the content is a link:
            Paragraph paragraph = new Paragraph();
            paragraph.Add("iText at the ");
            Link chunk = new Link("European Business Awards",
                PdfAction.CreateURI("https://itextpdf.com/en/events/itext-european-business-awards-gala-milan"));
            paragraph.Add(chunk);
            paragraph.Add(" gala in Milan");
            table.AddCell(paragraph);

            // The complete cell is a link:
            Cell cell = new Cell().Add(new Paragraph("Help us win a European Business Award!"));
            cell.SetNextRenderer(new LinkInCellRenderer(cell,
                "http://itextpdf.com/blog/help-us-win-european-business-award"));
            table.AddCell(cell);
            
            // The complete cell is a link (using SetAction() directly on cell):
            cell = new Cell().Add(new Paragraph(
                "IText becomes Belgium’s National Public Champion in the 2016 European Business Awards"));
            cell.SetAction(PdfAction.CreateURI(
                "http://itextpdf.com/en/blog/itext-becomes-belgiums-national-public-champion-2016-european-business-awards"));
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }

        private class LinkInCellRenderer : CellRenderer
        {
            private string url;

            public LinkInCellRenderer(Cell modelElement, string url)
                : base(modelElement)
            {
                this.url = url;
            }            
            
            // If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
            // If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
            // renderer will be created
            public override IRenderer GetNextRenderer()
            {
                return new LinkInCellRenderer((Cell) modelElement, url);
            }

            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);

                PdfLinkAnnotation linkAnnotation = new PdfLinkAnnotation(GetOccupiedAreaBBox());
                linkAnnotation.SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
                linkAnnotation.SetAction(PdfAction.CreateURI(url));
                drawContext.GetDocument().GetLastPage().AddAnnotation(linkAnnotation);
            }
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.