Skip to main content
Skip table of contents

Fix for when a link occupies more than two areas, a NullPointerException is thrown

Introduction

In versions of iText 7 Core prior to 7.1.12 it was possible that a link could occupy more than two areas. If this occurred, the result would be that a link occupying more than two areas would throw a NullPointerException.

After further investigation we found the source of the bug was caused due to a link annotation being copied and applied only on the draw method.

Solution

Our solution to this problem was to do the following:

    1. Copy the link annotation on split.

    2. Change the value of LINK_ANNOTATION property on each split (important for links which occupy more than two areas.)

        This is because when we update the second instance of a link, the content of the first one will be flushed.

Example Code

Below is some example code that demonstrates that the bug has been fixed and is working:

JAVA

JAVA
package LinkExample;

import com.itextpdf.kernel.colors.ColorConstants;
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.Div;

import java.io.FileNotFoundException;

public class LinkExample {

    public static void main(String[] args) throws FileNotFoundException {
        createPdf();
    }

    public static void createPdf() throws FileNotFoundException {
        //get ouput path for pdf file
        String outFileName = System.getProperty("user.dir")+"\\output.pdf";
        // create Pdfdocument with pdfwriter
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
        //add to doc
        Document doc = new Document(pdfDocument);
        //create link
        PdfAction action = PdfAction.createURI("http://itextpdf.com");
        //create div with height 2000 will expand to three pdf pages
        Div div = new Div()
                .setHeight(2000)
                .setBackgroundColor(ColorConstants.RED);
        //add link to pdf
        div.setAction(action);
        //add div to document
        doc.add(div);
        //close document
        doc.close();
    }
}

C#

C#
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
    class MutipleLinkExample{
        public static void Main(String[]args)
        {
            CreatePdf();
        }
        public static void CreatePdf()
        {
            // path to output pdf
            String outFileName = Directory.GetCurrentDirectory()+ "\\output.pdf";
            
            // create Pdfdocument with pdfwriter
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
            //add to doc
            Document doc = new Document(pdfDocument);
            //create link
            PdfAction action = PdfAction.CreateURI("http://itextpdf.com");
            //create div with height 2000 will expand to three pdf pages
            Div div = new Div()
                    .SetHeight(2000)
                    .SetBackgroundColor(ColorConstants.RED);
            //add link to pdf
            div.SetAction(action);
            //add div to document
            doc.Add(div);
            //close document
            doc.Close();
        }
 



JavaScript errors detected

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

If this problem persists, please contact our support.