Skip to content

Commit 94bd7fb

Browse files
HTML and Image rendering fixes
1 parent 5f62c22 commit 94bd7fb

File tree

1 file changed

+110
-15
lines changed

1 file changed

+110
-15
lines changed

dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs

Lines changed: 110 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using iText.Layout.Borders;
2323
using iText.Layout.Element;
2424
using iText.Layout.Font;
25+
using iText.Layout.Layout;
2526
using iText.Layout.Properties;
2627
using log4net;
2728
using NetTopologySuite.Utilities;
@@ -407,6 +408,7 @@ void GxDrawRect(int left, int top, int right, int bottom, int pen, Color foreCol
407408
public override void GxDrawLine(int left, int top, int right, int bottom, int width, int foreRed, int foreGreen, int foreBlue, int style)
408409
{
409410
PdfCanvas cb = new PdfCanvas(pdfPage);
411+
410412
Color foreColor = new DeviceRgb(foreRed, foreGreen, foreBlue);
411413

412414
float widthAux = (float)convertScale(width);
@@ -447,6 +449,7 @@ public override void GxDrawLine(int left, int top, int right, int bottom, int wi
447449
}
448450
public override void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom, int aspectRatio)
449451
{
452+
450453
try
451454
{
452455
Image image;
@@ -497,7 +500,7 @@ public override void GxDrawBitMap(String bitmap, int left, int top, int right, i
497500
float bottomAux = (float)convertScale(bottom);
498501
float leftAux = (float)convertScale(left);
499502
float topAux = (float)convertScale(top);
500-
image.SetFixedPosition(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin);
503+
image.SetFixedPosition(this.getPage(),leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin);
501504
if (aspectRatio == 0)
502505
image.ScaleAbsolute(rightAux - leftAux, bottomAux - topAux);
503506
else
@@ -769,26 +772,30 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
769772
bottomAux = (float)convertScale(bottom);
770773
topAux = (float)convertScale(top);
771774

772-
773-
Canvas cb = new Canvas(canvas, new Rectangle(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin, rightAux - leftAux, bottomAux - topAux));
775+
Rectangle htmlRectangle = new Rectangle(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin, rightAux - leftAux, bottomAux - topAux);
776+
Canvas cb = new Canvas(canvas, htmlRectangle);
774777
TextAlignment colAlignment = (TextAlignment)GetTextAlignment(alignment);
775778
cb.SetTextAlignment(colAlignment);
776779

777780
try
778781
{
779-
IList<IElement> objects = HtmlConverter.ConvertToElements(sTxt, properties);
782+
bottomAux = (float)convertScale(bottom);
783+
topAux = (float)convertScale(top);
784+
float drawingPageHeight = this.pageSize.GetTop() - topMargin - bottomMargin;
780785

781-
foreach (IElement element in objects)
782-
{
783-
Paragraph p = element as Paragraph;
784-
if (p != null)
785-
{
786-
if (alignment != 0)
787-
p.SetTextAlignment(colAlignment);
788-
}
789-
790-
cb.Add((IBlockElement)element);
791-
}
786+
float llx = leftAux + leftMargin;
787+
float lly = drawingPageHeight - bottomAux;
788+
float urx = rightAux + leftMargin;
789+
float ury = drawingPageHeight - topAux;
790+
791+
YPosition yPosition = new YPosition(htmlRectangle.GetTop());
792+
793+
ConverterProperties converterProperties = new ConverterProperties();
794+
converterProperties.SetFontProvider(document.GetFontProvider());
795+
//Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly
796+
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, properties);
797+
foreach (IElement element in elements)
798+
processHTMLElement(cb, colAlignment, htmlRectangle, yPosition, (IBlockElement)element);
792799
}
793800
catch (Exception ex1)
794801
{
@@ -974,6 +981,94 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
974981

975982
}
976983

984+
private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
985+
{
986+
Div div = blockElement as Div;
987+
if (div != null) {
988+
// Iterate through the children of the Div and process each child element recursively
989+
foreach (IElement child in div.GetChildren())
990+
if (child is IBlockElement)
991+
processHTMLElement(cb, colAlignment, htmlRectangle, currentYPosition, (IBlockElement)child);
992+
993+
}
994+
995+
float blockElementHeight = getBlockElementHeight(blockElement, htmlRectangle);
996+
float availableSpace = currentYPosition.CurrentYPosition - htmlRectangle.GetBottom();
997+
if (blockElementHeight > availableSpace)
998+
{
999+
GXLogging.Error(log, "You are trying to render an element of height " + blockElementHeight + " in a space of height " + availableSpace);
1000+
return;
1001+
}
1002+
1003+
Link anchor = blockElement as Link;
1004+
if (anchor != null)
1005+
{
1006+
anchor.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1007+
document.Add((IBlockElement) anchor);
1008+
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1009+
return;
1010+
}
1011+
1012+
Paragraph p = blockElement as Paragraph;
1013+
if (p != null)
1014+
{
1015+
p.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1016+
document.Add(p);
1017+
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1018+
return;
1019+
}
1020+
1021+
Table table = blockElement as Table;
1022+
if (table != null)
1023+
{
1024+
table.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1025+
table.SetTextAlignment(colAlignment);
1026+
cb.Add(table);
1027+
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1028+
return;
1029+
}
1030+
1031+
List list = blockElement as List;
1032+
if (list != null)
1033+
{
1034+
// This is a hack for the specific case of rendering a list as cb.Add(list) fails to add numeration to each element but document.Add(list) fails to
1035+
// consider the numeration of each element as part of it. Solution is to use document.Add(list) and move the list to the right.
1036+
float numWidth = new Paragraph("1. ").CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight();
1037+
list.SetFixedPosition(this.getPage(), htmlRectangle.GetX() + numWidth, currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1038+
1039+
list.SetTextAlignment(colAlignment);
1040+
document.Add(list);
1041+
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1042+
return;
1043+
}
1044+
1045+
Image image = blockElement as Image;
1046+
if (image != null)
1047+
{
1048+
image.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1049+
image.SetTextAlignment(colAlignment);
1050+
document.Add(image);
1051+
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1052+
return;
1053+
}
1054+
1055+
}
1056+
1057+
private float getBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle)
1058+
{
1059+
return blockElement.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight();
1060+
}
1061+
1062+
private class YPosition
1063+
{
1064+
public YPosition(float initialYPosition)
1065+
{
1066+
CurrentYPosition = initialYPosition;
1067+
}
1068+
1069+
public float CurrentYPosition { get; set; }
1070+
}
1071+
9771072
private BaseDirection? GetBaseDirection(int runDirection)
9781073
{
9791074
switch (runDirection)

0 commit comments

Comments
 (0)