Skip to content

Commit 8dd81f3

Browse files
Some of the <div> elements generated by HtmlConverter.ConvertToElements do not contain any child elements; instead, they solely contain formatting properties (e.g., <h1>, horizontal rule <hr>). Rendering these <div> elements results in a format closer to the original HTML. However, these changes mean that if a <div> extends beyond the available drawing area, it won't be displayed. This was the previous behavior.
1 parent ef31954 commit 8dd81f3

File tree

1 file changed

+27
-45
lines changed

1 file changed

+27
-45
lines changed

dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext8.cs

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Globalization;
55
using System.IO;
6+
using System.Linq;
67
using System.Text;
78
using GeneXus;
89
using iText.Barcodes;
@@ -805,14 +806,27 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
805806
Rectangle htmlRectangle = new Rectangle(llx, lly, urx - llx, ury - lly);
806807
YPosition yPosition = new YPosition(htmlRectangle.GetTop());
807808

808-
PdfCanvas htmlPdfCanvas = new PdfCanvas(pdfPage);
809809
Canvas htmlCanvas = new Canvas(canvas, htmlRectangle);
810-
810+
htmlCanvas.SetFontProvider(fontProvider);
811811

812812
//Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly
813813
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, converterProperties);
814814
foreach (IElement element in elements)
815-
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element);
815+
{
816+
bool fitInPage = ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element, htmlCanvas, drawingPageHeight);
817+
if (!fitInPage)
818+
{
819+
GxEndPage();
820+
GxStartPage();
821+
htmlCanvas.Close();
822+
float remainingDrawingPageHeight = bottomAux - drawingPageHeight;
823+
htmlRectangle.SetY(drawingPageHeight - (remainingDrawingPageHeight));
824+
htmlCanvas = new Canvas(canvas, htmlRectangle);
825+
htmlCanvas.SetFontProvider(fontProvider);
826+
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element, htmlCanvas, remainingDrawingPageHeight);
827+
}
828+
}
829+
816830
}
817831
catch (Exception ex1)
818832
{
@@ -954,56 +968,24 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
954968
}
955969
}
956970

957-
private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
971+
private bool ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement, Canvas canvas, float drawingPageHeight)
958972
{
959-
Div div = blockElement as Div;
960-
if (div != null)
961-
{
962-
// Iterate through the children of the Div and process each child element recursively
963-
foreach (IElement child in div.GetChildren())
964-
if (child is IBlockElement)
965-
ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child);
966-
967-
}
968973

969974
float blockElementHeight = GetBlockElementHeight(blockElement, htmlRectangle);
970975
float availableSpace = currentYPosition.CurrentYPosition - htmlRectangle.GetBottom();
971-
if (blockElementHeight > availableSpace)
972-
{
973-
GXLogging.Error(log, "You are trying to render an element of height " + blockElementHeight + " in a space of height " + availableSpace);
974-
return;
975-
}
976-
977-
if (blockElement is Paragraph p)
976+
if (PageHeightExceeded(blockElementHeight, availableSpace))
978977
{
979-
p.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
980-
document.Add(p);
981-
}
982-
else if (blockElement is Table table)
983-
{
984-
table.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
985-
document.Add(table);
986-
}
987-
else if (blockElement is List list)
988-
{
989-
list.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
990-
document.Add(list);
991-
}
992-
else if (blockElement is Link anchor)
993-
{
994-
anchor.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
995-
document.Add((IBlockElement)anchor);
996-
}
997-
else if (blockElement is Image image)
998-
{
999-
image.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1000-
document.Add(image);
978+
GXLogging.Warn(log, "You are trying to render an element of height " + blockElementHeight + " in a space of height " + availableSpace);
979+
return false;
1001980
}
981+
canvas.Add(blockElement);
1002982
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1003-
1004-
return;
983+
return true;
984+
}
985+
bool PageHeightExceeded(float bottomAux, float drawingPageHeight)
986+
{
987+
return bottomAux > drawingPageHeight;
1005988
}
1006-
1007989
private float GetBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle)
1008990
{
1009991
return blockElement.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight();

0 commit comments

Comments
 (0)