Skip to content

Commit

Permalink
Fixed text wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-sexenian committed Sep 19, 2023
1 parent 404a2f9 commit ea70e91
Showing 1 changed file with 69 additions and 16 deletions.
85 changes: 69 additions & 16 deletions dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
using iText.Layout.Font;
using iText.Layout.Layout;
using iText.Layout.Properties;
using iText.Layout.Splitting;
using log4net;
using NetTopologySuite.Utilities;
using static iText.Kernel.Pdf.Colorspace.PdfPattern;
using Path = System.IO.Path;
using Text = iText.Layout.Element.Text;

Expand Down Expand Up @@ -955,7 +957,6 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b

if (wrap || justified)
{

bottomAux = (float)convertScale(bottomOri);
topAux = (float)convertScale(topOri);

Expand All @@ -969,7 +970,7 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
float urx = rightAux + leftMargin;
float ury = (float)this.pageSize.GetTop() - topAux - topMargin - bottomMargin;

DrawColumnText(canvas, llx, lly, urx, ury, p, leading, runDirection, valign, alignment);
DrawColumnText(canvas, llx, lly, urx, ury, p, leading, runDirection, valign, alignment, wrap);

}
else //no wrap
Expand All @@ -992,7 +993,6 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
text.AddStyle(fontStyle);

Paragraph phrase = new Paragraph(text).SetVerticalAlignment(VerticalAlignment.TOP).SetFontColor(foreColor);

switch (alignment)
{
case 1: // Center Alignment
Expand All @@ -1006,7 +1006,7 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
break;
case 3: // Justified, only one text line
cb.ShowTextAligned(phrase, leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin + startHeight, justifiedType);//0, runDirection, arabicOptions);
break;
break;
}
}
}
Expand Down Expand Up @@ -1131,19 +1131,72 @@ private VerticalAlignment GetVericalAlignment(float valign)
return null;
}

void DrawColumnText(PdfCanvas pdfCanvas, float llx, float lly, float urx, float ury, Paragraph p, float leading, int runDirection, int valign, int alignment)
void DrawColumnText(PdfCanvas pdfCanvas, float llx, float lly, float urx, float ury, Paragraph p, float leading, int runDirection, int valign, int alignment, Boolean wrap)
{
float width = urx - llx;
float height = ury - lly;

p.SetTextAlignment(GetTextAlignment(alignment))
.SetVerticalAlignment(GetVericalAlignment(valign))
.SetMultipliedLeading(MULTIPLIED_LEADING)
.SetWidth(width)
.SetMaxHeight(height);
//.SetMargin(10);
Canvas cb = new Canvas(pdfCanvas, new Rectangle(llx, lly, width, height));
cb.Add(p);
if (VerticalAlign.MIDDLE.Equals(valign))
{
ury = ury + leading;
p.SetVerticalAlignment(VerticalAlignment.MIDDLE);
}
else if (VerticalAlign.BOTTOM.Equals(valign))
{
ury = ury + leading;
p.SetVerticalAlignment(VerticalAlignment.BOTTOM);
}
else if (VerticalAlign.TOP.Equals(valign))
{
ury = ury + leading / 2;
p.SetVerticalAlignment(VerticalAlignment.TOP);
}
Rectangle rect = new Rectangle(llx, lly, urx - llx, ury - lly);
p.SetTextAlignment(GetTextAlignment(alignment));

if (wrap)
{
p.SetProperty(Property.SPLIT_CHARACTERS, new CustomSplitCharacters());
Table table = new Table(1);
table.SetFixedPosition(this.getPage(), rect.GetX(), rect.GetY(), rect.GetWidth());
Cell cell = new Cell();
cell.SetWidth(rect.GetWidth());
cell.SetHeight(rect.GetHeight());
cell.SetBorder(Border.NO_BORDER);
cell.SetVerticalAlignment(VerticalAlignment.MIDDLE);
cell.Add(p);
table.AddCell(cell);
document.Add(table);
}
else
{
try
{
Canvas canvas = new Canvas(pdfCanvas, rect);
canvas.Add(p);
canvas.Close();
}
catch (Exception e) { GXLogging.Error(log, "GxDrawText failed to justify text column: ", e); }
}
}

public class CustomSplitCharacters : DefaultSplitCharacters
{
public override bool IsSplitCharacter(GlyphLine text, int glyphPos)
{
if (!text.Get(glyphPos).HasValidUnicode())
{
return false;
}

bool baseResult = base.IsSplitCharacter(text, glyphPos);
bool myResult = false;
Glyph glyph = text.Get(glyphPos);

if (glyph.GetUnicode() == '_')
{
myResult = true;
}

return myResult || baseResult;
}
}

#pragma warning restore CS0612 // Type or member is obsolete
Expand Down

0 comments on commit ea70e91

Please sign in to comment.