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
I'm working on a custom PDF text layout using OpenPDF ColumnText and need to render multiple phrases, each with its own leading, character spacing, alignment, and styling. Currently, I use ct.addElement(Paragraph) for each phrase, which gives me paragraph-level leading and alignment but does not produce the desired inline text flow (phrases stack vertically with individual settings but don't flow as inline text). When I use ct.addText(Chunk) with manual positioning, I get inline rendering, but lose distinct leading and alignment for each phrase.
Here is the rendering I want to achieve (see third image, ignore font and color for now):
- Each phrase should have its own alignment (e.g. left, right), leading, and character spacing.
- Phrases should flow inline, respecting their individual settings, and wrap to the next line if needed.
What I've tried
Using ct.addElement(Paragraph) for each phrase: I get individual leading and alignment but phrases appear stacked, not flowing inline.
Using ct.addText(Chunk) for inline rendering: All phrases are in one paragraph, making it impossible to set individual alignment/leading for each phrase.
How can I achieve both paragraph-level control (leading, alignment per phrase) and inline text flow in ColumnText? Is there a workaround, or do I need to manually measure and position each phrase chunk?
Is there an OpenPDF idiom or workaround to achieve this, or do I need to process and position the phrases manually?
`public void draw() {
float llx = 41;
float lly = 60;
float urx = 289;
float ury = 191;
try (Document document = new Document(new Rectangle(llx, lly, urx, ury), 0, 0, 0, 0)) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, os);
writer.setViewerPreferences(PdfWriter.PrintScalingNone);
document.open();
PdfContentByte cb = writer.getDirectContent();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(43, 61, 288, 189);
BaseFont bf = BaseFont.createFont();
ct.setUseAscender(false);
Font font = new Font(bf, 12);
font.setColor(Color.GREEN);
float dAscent = font.getBaseFont().getAscentPoint("d", font.getSize());
System.out.println("defined dAscent baseline " + (ury - dAscent));
ct.setYLine(ury - dAscent);
ct.useDefaultFirstYLine(false);
ct.setFirstLineY(ury - dAscent);
String text1 = "Together ";
Chunk chunk1 = new Chunk(text1, font);
chunk1.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 1, Color.red);
chunk1.setCharacterSpacing(0.4 f);
chunk1.setHorizontalScaling(1);
Paragraph paragraph1 = new Paragraph(chunk1);
paragraph1.setLeading(14);
paragraph1.setAlignment(Element.ALIGN_RIGHT);
ct.addElement(paragraph1);
String text2 = "with their Parents";
Chunk chunk2 = new Chunk(text2, font);
chunk2.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 1, Color.GREEN);
chunk2.setCharacterSpacing(0.3 f);
chunk2.setHorizontalScaling(1);
Paragraph paragraph2 = new Paragraph(chunk2);
paragraph2.setLeading(12);
paragraph2.setAlignment(Element.ALIGN_LEFT);
ct.addElement(paragraph2);
String text3 = "Bella Sydney and george Thomas invite";
Chunk chunk3 = new Chunk(text3, font);
chunk3.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 1, Color.PINK);
chunk3.setCharacterSpacing(0.3 f);
chunk3.setHorizontalScaling(1);
Paragraph paragraph3 = new Paragraph(chunk3);
paragraph3.setLeading(12);
paragraph3.setAlignment(Element.ALIGN_RIGHT);
ct.addElement(paragraph3);
ct.go();
System.out.println("Alignment " + ct.getAlignment());
document.close();
File outputPDF = new File("target/columnTextUserBaselineTest.pdf");
FileOutputStream fos = new FileOutputStream(outputPDF);
fos.write(os.toByteArray());
fos.close();
}
'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on a custom PDF text layout using OpenPDF ColumnText and need to render multiple phrases, each with its own leading, character spacing, alignment, and styling. Currently, I use ct.addElement(Paragraph) for each phrase, which gives me paragraph-level leading and alignment but does not produce the desired inline text flow (phrases stack vertically with individual settings but don't flow as inline text). When I use ct.addText(Chunk) with manual positioning, I get inline rendering, but lose distinct leading and alignment for each phrase.
Here is the rendering I want to achieve (see third image, ignore font and color for now):
What I've tried
How can I achieve both paragraph-level control (leading, alignment per phrase) and inline text flow in ColumnText? Is there a workaround, or do I need to manually measure and position each phrase chunk?
Is there an OpenPDF idiom or workaround to achieve this, or do I need to process and position the phrases manually?
Beta Was this translation helpful? Give feedback.
All reactions