You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
Describe the bug
Setting
.setKeepTogether(true)
on a Paragraph throwsjava.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
Stacktrace
System
Your real name
Brian Treich
The text was updated successfully, but these errors were encountered: