Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a setting to prevent clipping overflow content when printing large HTML elements. #1046

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 61 additions & 27 deletions dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,12 +736,9 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
if (printRectangle && (border == 1 || backFill))
GxDrawRect(left, top, right, bottom, border, foreColor, backFill ? 1 : 0, backColor, 0, 0, 0, 0, 0, 0, 0, 0);

PdfCanvas canvas = new PdfCanvas(pdfPage);
sTxt = sTxt.TrimEnd(TRIM_CHARS);

PdfFont font = baseFont;
canvas.SetFontAndSize(this.baseFont, fontSize);
canvas.SetFillColor(foreColor);
float captionHeight = baseFont.GetAscent(sTxt, fontSize) / 1000;
float rectangleWidth = baseFont.GetWidth(sTxt, fontSize);
float lineHeight = (1 / 1000) * (baseFont.GetAscent(sTxt, fontSize) - baseFont.GetDescent(sTxt, fontSize)) + (fontSize * 1.2f);
Expand Down Expand Up @@ -803,39 +800,66 @@ 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 canvas = new PdfCanvas(pdfPage);
canvas.SetFontAndSize(this.baseFont, fontSize);
canvas.SetFillColor(foreColor);

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)

if (clipOverflowHtmlContent)
{
float blockElementHeight = GetBlockElementHeight((IBlockElement)element, htmlRectangle);

if (PageHeightExceeded(bottomMargin, yPosition.CurrentYPosition))
foreach (IElement element in elements)
{
llx = leftAux + leftMargin;
lly = drawingPageHeight - bottomAux;
urx = rightAux + leftMargin;
ury = drawingPageHeight - topAux;
htmlRectangle = new Rectangle(llx, lly, urx - llx, ury - lly);
yPosition = new YPosition(htmlRectangle.GetTop());
bottomAux -= drawingPageHeight;
GxEndPage();
GxStartPage();

canvas = new PdfCanvas(pdfPage);
sTxt = sTxt.TrimEnd(TRIM_CHARS);
canvas.SetFontAndSize(this.baseFont, fontSize);
canvas.SetFillColor(foreColor);

htmlCanvas = new Canvas(canvas, htmlRectangle);
htmlCanvas.SetFontProvider(fontProvider);
float blockElementHeight = GetBlockElementHeight((IBlockElement)element, htmlRectangle);

if (PageHeightExceeded(bottomMargin, yPosition.CurrentYPosition))
{
llx = leftAux + leftMargin;
lly = drawingPageHeight - bottomAux;
urx = rightAux + leftMargin;
ury = drawingPageHeight - topAux;
htmlRectangle = new Rectangle(llx, lly, urx - llx, ury - lly);
yPosition = new YPosition(htmlRectangle.GetTop());
bottomAux -= drawingPageHeight;
GxEndPage();
GxStartPage();

canvas = new PdfCanvas(pdfPage);
sTxt = sTxt.TrimEnd(TRIM_CHARS);
canvas.SetFontAndSize(this.baseFont, fontSize);
canvas.SetFillColor(foreColor);

htmlCanvas = new Canvas(canvas, htmlRectangle);
htmlCanvas.SetFontProvider(fontProvider);
}
ProcessHTMLElement((IBlockElement)element, alignment, htmlCanvas);
yPosition.CurrentYPosition -= blockElementHeight;
}
ProcessHTMLElement((IBlockElement)element, alignment, htmlCanvas);
yPosition.CurrentYPosition -= blockElementHeight;
}

else
{
Div div = new Div();
div.SetMarginBottom(bottomMargin);
div.SetMarginTop(topAux + topMargin);
div.SetMarginLeft(leftAux);
div.SetMarginRight(pageSize.GetRight() - urx);
div.SetFont(this.baseFont);
div.SetFontSize(fontSize);
div.SetFontColor(foreColor);

foreach (IElement element in elements)
{
ProcessHTMLElement((IBlockElement)element, alignment, div);
}
document.Add(div);
}
pages = pdfDocument.GetNumberOfPages();
setPage(pages);
}
catch (Exception ex1)
{
Expand Down Expand Up @@ -978,13 +1002,23 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
}

private void ProcessHTMLElement(IBlockElement blockElement, int alignment, Canvas htmlCanvas)
{
SetAlignment(blockElement, alignment);
htmlCanvas.Add(blockElement);
}
private void ProcessHTMLElement(IBlockElement blockElement, int alignment, Div div)
{
SetAlignment(blockElement, alignment);
div.Add(blockElement);
}

private void SetAlignment(IBlockElement blockElement, int alignment)
{
if (blockElement is Paragraph p)
{
if (alignment != 0)
p.SetTextAlignment(GetTextAlignment(alignment));
}
htmlCanvas.Add(blockElement);
}
bool PageHeightExceeded(float bottomAux, float drawingPageHeight)
{
Expand Down
8 changes: 8 additions & 0 deletions dotnet/src/dotnetframework/GxPdfReportsCS/PDFReportCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public abstract class PDFReportBase : IReportHandler
protected int STYLE_NONE_CONST = 1;
protected int runDirection;
protected int MULTIPLIED_LEADING = 1;
protected bool clipOverflowHtmlContent = true;

public bool GxOpenDoc(string fileName)
{
Expand Down Expand Up @@ -337,6 +338,7 @@ private void loadProps()
props.setupGeneralProperty(Const.COMPLIANCE_LEVEL, PdfConformanceLevel.None.ToString());
props.setupGeneralProperty(Const.RUN_DIRECTION, Const.RUN_DIRECTION_LTR);
props.setupGeneralProperty(Const.JUSTIFIED_TYPE_ALL, "false");
props.setupGeneralProperty(Const.CLIP_OVERFLOW_HTML_CONTENT, "true");

props.setupGeneralProperty(Const.STYLE_DOTTED, Const.DEFAULT_STYLE_DOTTED);
props.setupGeneralProperty(Const.STYLE_DASHED, Const.DEFAULT_STYLE_DASHED);
Expand Down Expand Up @@ -634,6 +636,11 @@ public bool GxPrintInit(String output, ref int gxXPage, ref int gxYPage, String
STYLE_LONG_DASHED = parsePattern(props.getGeneralProperty(Const.STYLE_LONG_DASHED));
STYLE_LONG_DOT_DASHED = parsePattern(props.getGeneralProperty(Const.STYLE_LONG_DOT_DASHED));

if (props.getBooleanGeneralProperty(Const.CLIP_OVERFLOW_HTML_CONTENT, true))
clipOverflowHtmlContent = true;
else
clipOverflowHtmlContent = false;

int result;
if (Int32.TryParse(props.getGeneralProperty(Const.RUN_DIRECTION), out result))
runDirection = result;
Expand Down Expand Up @@ -1396,6 +1403,7 @@ public class Const
public static String RUN_DIRECTION_LTR = "2";

public static String JUSTIFIED_TYPE_ALL = "JustifiedTypeAll";
public static String CLIP_OVERFLOW_HTML_CONTENT = "ClipOverflowHtmlContent";

public const int OUTPUT_SCREEN = 0;
public const int OUTPUT_PRINTER = 1;
Expand Down
22 changes: 21 additions & 1 deletion dotnet/src/dotnetframework/GxPdfReportsCS/PDFReportItext4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,29 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
else
{
Col.AddElement((IElement)element);
Col.Go();
if (clipOverflowHtmlContent)
{
Col.Go();
}
else
{
int status;
do
{
status = Col.Go();
if (ColumnText.HasMoreText(status))
{
GxEndPage();
GxStartPage();
rect = new Rectangle(leftAux + leftMargin, drawingPageHeight - bottomAux, rightAux + leftMargin, drawingPageHeight - topAux);
SetSimpleColumn(Col, rect);
}
} while (ColumnText.HasMoreText(status));
}
}
}
pages = document.PageNumber;
setPage(pages);
if (drawWithTable)
{
ClassLoader.Invoke(t, "AddCell", new object[] { cell });
Expand Down
Loading