Skip to content

Commit

Permalink
Fix the page break issue with BLOCK mode; #36
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed May 30, 2024
1 parent ca67e28 commit 2778ae0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
Binary file modified example-files/pltext/horz-alignment.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -647,29 +647,28 @@ protected void onRender (@Nonnull final PageRenderContext aCtx) throws IOExcepti
aContentStream.moveTextPositionByAmount (fIndentX, 0);
}

if (bDoTextJustify || (bDoTextBlock && !aTW.isDisplayAsNewline ()))
if ((bDoTextJustify && bBeforeLastLine) || (bDoTextBlock && !aTW.isDisplayAsNewline ()))
{
if (bBeforeLastLine)
// Justify the content of this line
// Avoid division by zero
float fCharSpacing = 0;
if (sDrawText.length () > 1)
{
// Avoid division by zero
float fCharSpacing = 0;
if (sDrawText.length () > 1)
{
// Calculate width of space between each character (therefore -1)
fCharSpacing = (fPreparedWidth - fTextWidth) / (sDrawText.length () - 1);
}

// Set for each line separately,
aContentStream.setCharacterSpacing (fCharSpacing);
// Calculate width of space between each character (therefore -1)
fCharSpacing = (fPreparedWidth - fTextWidth) / (sDrawText.length () - 1);
}
else

// Set for each line separately,
aContentStream.setCharacterSpacing (fCharSpacing);
}
else
if ((bDoTextJustify && !bBeforeLastLine) || (bDoTextBlock && aTW.isDisplayAsNewline ()))
{
// On last line, no justify
// No justification for that line
// Important to reset back to default after all (if any was set)
if (nIndex > 0)
aContentStream.setCharacterSpacing (0);
}
}

// Main draw string
aContentStream.drawString (sDrawText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.helger.pdflayout.element.box.PLBox;
import com.helger.pdflayout.element.hbox.PLHBox;
import com.helger.pdflayout.element.special.PLPageBreak;
import com.helger.pdflayout.element.special.PLSpacerY;
import com.helger.pdflayout.element.vbox.PLVBox;
import com.helger.pdflayout.spec.BorderStyleSpec;
import com.helger.pdflayout.spec.EHorzAlignment;
Expand Down Expand Up @@ -403,6 +404,16 @@ public void testTextHorzAlignment () throws PDFCreationException
aPS1.addElement (aHBox);
}

{
aPS1.addElement (new PLSpacerY (20));
final String s2 = "!" +
StringHelper.getRepeated ("Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste.",
50);
final PLHBox aHBox = new PLHBox ();
aHBox.addColumn (new PLText (s2, r10).setHorzAlign (EHorzAlignment.BLOCK), WidthSpec.perc (50));
aPS1.addElement (aHBox);
}

final PageLayoutPDF aPageLayout = new PageLayoutPDF ();
aPageLayout.addPageSet (aPS1);
PDFTestComparer.renderAndCompare (aPageLayout, new File ("pdf/pltext/horz-alignment.pdf"));
Expand Down

0 comments on commit 2778ae0

Please sign in to comment.