Skip to main content
Skip table of contents

Page events for Paragraphs

These examples were written in response to the question Click How to add a border to a paragraph?


borderforparagraph

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

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class BorderForParagraph {
    public static final String DEST = "./target/sandbox/events/border_for_paragraph.pdf";

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

        new BorderForParagraph().manipulatePdf(DEST);
    }

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

        doc.add(new Paragraph("Hello,"));
        doc.add(new Paragraph("In this doc, we'll add several paragraphs that will trigger page events. " +
                "As long as the event isn't activated, nothing special happens, " +
                "but let's make the event active and see what happens:"));

        Paragraph paragraphWithBorder = new Paragraph("This paragraph now has a border. Isn't that fantastic? " +
                "By changing the event, we can even provide a background color, " +
                "change the line width of the border and many other things. Now let's deactivate the event.");

        // There were no method that allows you to create a border for a Paragraph, since iText5 is EOL.
        // In iText a border for a Paragraph can be created by calling setBorder() method.
        paragraphWithBorder.setBorder(new SolidBorder(1));
        doc.add(paragraphWithBorder);

        doc.add(new Paragraph("This paragraph no longer has a border."));

        doc.close();
    }
}

C#

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

namespace iText.Samples.Sandbox.Events
{
    public class BorderForParagraph
    {
        public static readonly String DEST = "results/sandbox/events/border_for_paragraph.pdf";

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

            new BorderForParagraph().ManipulatePdf(DEST);
        }

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

            doc.Add(new Paragraph("Hello,"));
            doc.Add(new Paragraph("In this doc, we'll add several paragraphs that will trigger page events. " +
                                  "As long as the event isn't activated, nothing special happens, " +
                                  "but let's make the event active and see what happens:"));

            Paragraph paragraphWithBorder = new Paragraph("This paragraph now has a border. Isn't that fantastic? " +
                                                        "By changing the event, we can even provide a background color, " +
                                                        "change the line width of the border and many other things. Now let's deactivate the event.");

            // There were no method that allows you to create a border for a Paragraph, since iText5 is EOL.
            // In iText a border for a Paragraph can be created by calling setBorder() method.
            paragraphWithBorder.SetBorder(new SolidBorder(1));
            doc.Add(paragraphWithBorder);

            doc.Add(new Paragraph("This paragraph no longer has a border."));

            doc.Close();
        }
    }
}


borderforparagraph2

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

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class BorderForParagraph2 {
    public static final String DEST = "./target/sandbox/events/border_for_paragraph2.pdf";

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

        new BorderForParagraph2().manipulatePdf(DEST);
    }

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

        doc.add(new Paragraph("Hello,"));
        doc.add(new Paragraph("In this doc, we'll add several paragraphs that will trigger page events. " +
                "As long as the event isn't activated, nothing special happens, " +
                "but let's make the event active and see what happens:"));

        Paragraph paragraphWithBorder = new Paragraph("This paragraph now has a border. " +
                "Isn't that fantastic? By changing the event, we can even provide a background color, " +
                "change the line width of the border and many other things. Now let's deactivate the event.");

        // There were no method that allows you to create a border for a Paragraph, since iText5 is EOL.
        // In iText a border for a Paragraph can be created by calling setBorder() method.
        paragraphWithBorder.setBorder(new SolidBorder(1));
        doc.add(paragraphWithBorder);
        doc.add(new Paragraph("This paragraph no longer has a border."));

        doc.add(new Paragraph("Let's repeat:"));
        for (int i = 0; i < 10; i++) {
            paragraphWithBorder = new Paragraph("This paragraph now has a border. Isn't that fantastic? " +
                    "By changing the event, we can even provide a background color, " +
                    "change the line width of the border and many other things. Now let's deactivate the event.");
            paragraphWithBorder.setBorder(new SolidBorder(1));
            doc.add(paragraphWithBorder);
            doc.add(new Paragraph("This paragraph no longer has a border."));
        }

        doc.close();
    }
}

C#

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

namespace iText.Samples.Sandbox.Events
{
    public class BorderForParagraph2
    {
        public static readonly String DEST = "results/sandbox/events/border_for_paragraph2.pdf";

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

            new BorderForParagraph2().ManipulatePdf(DEST);
        }

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

            doc.Add(new Paragraph("Hello,"));
            doc.Add(new Paragraph("In this doc, we'll add several paragraphs that will trigger page events. " +
                                  "As long as the event isn't activated, nothing special happens, " +
                                  "but let's make the event active and see what happens:"));

            Paragraph paragraphWithBorder = new Paragraph("This paragraph now has a border. " +
                                                        "Isn't that fantastic? By changing the event, we can even provide a background color, " +
                                                        "change the line width of the border and many other things. Now let's deactivate the event.");

            // There were no method that allows you to create a border for a Paragraph, since iText5 is EOL.
            // In iText a border for a Paragraph can be created by calling setBorder() method.
            paragraphWithBorder.SetBorder(new SolidBorder(1));
            doc.Add(paragraphWithBorder);
            doc.Add(new Paragraph("This paragraph no longer has a border."));

            doc.Add(new Paragraph("Let's repeat:"));
            for (int i = 0; i < 10; i++)
            {
                paragraphWithBorder = new Paragraph("This paragraph now has a border. Isn't that fantastic? " +
                                                  "By changing the event, we can even provide a background color, " +
                                                  "change the line width of the border and many other things. Now let's deactivate the event.");
                paragraphWithBorder.SetBorder(new SolidBorder(1));
                doc.Add(paragraphWithBorder);
                doc.Add(new Paragraph("This paragraph no longer has a border."));
            }

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

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

If this problem persists, please contact our support.