Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting KeepTogether on paragraphs with an anchor throws an exception #1228

Open
brian93847 opened this issue Oct 23, 2024 · 0 comments
Open
Labels

Comments

@brian93847
Copy link

brian93847 commented Oct 23, 2024

Describe the bug

Setting .setKeepTogether(true) on a Paragraph throws java.lang.IllegalArgumentException: Element not allowed if the Paragraph object contains any Anchor objects. I consider this a bug because by default Paragraphs may contain Anchor objects and the rest of Paragraph's methods seem to work regardless if the Paragraph contains an anchor or not; this seems to be Paragraph's only method that errors out if it contains an Anchor. Also, conceptually it seems like a paragraph should be able to have anchors within it even if they are being kept together.

This exception is thrown by ColumnText's addElement method. Notably the method comment doesn't list Anchors as a supported element so it seems like it's not a bug per se of that method but instead of the code calling it even thought the calling code may contain unsupported elements. So a solution could be to add Anchor support to ColumnText's addElement method or to adjust the calling code to not rely on that method.

To Reproduce

Code to reproduce the issue

import com.lowagie.text.Anchor;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class EleNotAllowed {

    public static void main(String[] args) throws DocumentException, FileNotFoundException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
        document.open();
        Paragraph paragraph1 = new Paragraph("paragraph 1");
        Paragraph paragraph2 = new Paragraph("paragraph 2");
        Anchor anchor1 = new Anchor("this is a link");
        anchor1.setName("LINK");
        anchor1.setReference("http://www.example.com");
        Paragraph paragraph3 = new Paragraph("paragraph 3");
        Paragraph mainParagraph = new Paragraph();
        mainParagraph.add(paragraph1);
        mainParagraph.add(paragraph2);
        mainParagraph.add(anchor1);
        mainParagraph.add(paragraph3);

        /*
        Uncommenting the line below throws
        java.lang.IllegalArgumentException: Element not allowed
        from ColumnText's addElement method
         */
        mainParagraph.setKeepTogether(true);

        document.add(mainParagraph);
        document.close();
    }

}

Stacktrace

Exception in thread "main" com.lowagie.text.DocumentException: java.lang.IllegalArgumentException: Element not allowed.
	at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:808)
	at com.lowagie.text.Document.add(Document.java:303)
	at com.lowagie.examples.EleNotAllowed.main(EleNotAllowed.java:36)
Caused by: java.lang.IllegalArgumentException: Element not allowed.
	at com.lowagie.text.pdf.ColumnText.addElement(ColumnText.java:462)
	at com.lowagie.text.pdf.PdfPCell.addElement(PdfPCell.java:273)
	at com.lowagie.text.pdf.PdfDocument.createInOneCell(PdfDocument.java:848)
	at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:560)
	... 2 more

System

  • OpenPDF version: 2.0.1

Your real name

Brian Treich

@brian93847 brian93847 added the bug label Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant