Skip to content

Commit

Permalink
Some of the <div> elements generated by HtmlConverter.ConvertToElemen…
Browse files Browse the repository at this point in the history
…ts 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.
  • Loading branch information
claudiamurialdo committed Sep 26, 2023
1 parent ef31954 commit 8dd81f3
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using GeneXus;
using iText.Barcodes;
Expand Down Expand Up @@ -805,14 +806,27 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
Rectangle htmlRectangle = new Rectangle(llx, lly, urx - llx, ury - lly);
YPosition yPosition = new YPosition(htmlRectangle.GetTop());

PdfCanvas htmlPdfCanvas = new PdfCanvas(pdfPage);
Canvas htmlCanvas = new Canvas(canvas, htmlRectangle);

htmlCanvas.SetFontProvider(fontProvider);

//Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, converterProperties);
foreach (IElement element in elements)
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element);
{
bool fitInPage = ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element, htmlCanvas, drawingPageHeight);
if (!fitInPage)
{
GxEndPage();
GxStartPage();
htmlCanvas.Close();
float remainingDrawingPageHeight = bottomAux - drawingPageHeight;
htmlRectangle.SetY(drawingPageHeight - (remainingDrawingPageHeight));
htmlCanvas = new Canvas(canvas, htmlRectangle);
htmlCanvas.SetFontProvider(fontProvider);
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element, htmlCanvas, remainingDrawingPageHeight);
}
}

}
catch (Exception ex1)
{
Expand Down Expand Up @@ -954,56 +968,24 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
}
}

private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
private bool ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement, Canvas canvas, float drawingPageHeight)
{
Div div = blockElement as Div;
if (div != null)
{
// Iterate through the children of the Div and process each child element recursively
foreach (IElement child in div.GetChildren())
if (child is IBlockElement)
ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child);

}

float blockElementHeight = GetBlockElementHeight(blockElement, htmlRectangle);
float availableSpace = currentYPosition.CurrentYPosition - htmlRectangle.GetBottom();
if (blockElementHeight > availableSpace)
{
GXLogging.Error(log, "You are trying to render an element of height " + blockElementHeight + " in a space of height " + availableSpace);
return;
}

if (blockElement is Paragraph p)
if (PageHeightExceeded(blockElementHeight, availableSpace))
{
p.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
document.Add(p);
}
else if (blockElement is Table table)
{
table.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
document.Add(table);
}
else if (blockElement is List list)
{
list.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
document.Add(list);
}
else if (blockElement is Link anchor)
{
anchor.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
document.Add((IBlockElement)anchor);
}
else if (blockElement is Image image)
{
image.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
document.Add(image);
GXLogging.Warn(log, "You are trying to render an element of height " + blockElementHeight + " in a space of height " + availableSpace);
return false;
}
canvas.Add(blockElement);
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;

return;
return true;
}
bool PageHeightExceeded(float bottomAux, float drawingPageHeight)
{
return bottomAux > drawingPageHeight;
}

private float GetBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle)
{
return blockElement.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight();
Expand Down

0 comments on commit 8dd81f3

Please sign in to comment.