From 50b16effbab69b0464b5a78e0f3edf3187139832 Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Mon, 25 Sep 2023 10:14:48 -0300 Subject: [PATCH] Cherry pick branch 'genexuslabs:itext8' into beta --- .../GxPdfReportsCS/PDFReportItext7.cs | 97 ++++--------------- 1 file changed, 20 insertions(+), 77 deletions(-) diff --git a/dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs b/dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs index 89ff34c7f..0370498b4 100644 --- a/dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs +++ b/dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs @@ -44,7 +44,6 @@ public GxReportBuilderPdf7(string appPath, Stream outputStream) _pdfReport = new com.genexus.reports.PDFReportItextSharp7(appPath); if (outputStream != null) { - _pdfReport.setOutputStream(outputStream); GXLogging.Debug(log, "GxReportBuilderPdf outputStream: binaryWriter"); } @@ -108,7 +107,6 @@ protected override void init(ref int gxYPage, ref int gxXPage, int pageWidth, in pdfDocument.SetDefaultPageSize(this.pageSize); document = new Document(pdfDocument); - } catch (PdfException de) { @@ -622,7 +620,6 @@ public override void GxAttris(String fontName, int fontSize, bool fontBold, bool break; } } - baseFont = PdfFontFactory.CreateFont(fontName, PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED); } } @@ -634,7 +631,6 @@ public override void GxAttris(String fontName, int fontSize, bool fontBold, bool fontStyle = new Style(); if (fontItalic) fontStyle.SetItalic(); if (fontBold) fontStyle.SetBold(); - } string fontPath = GetFontLocation(fontName); bool foundFont = true; @@ -707,7 +703,6 @@ private PdfFont CreateDefaultFont() else defaultFont = PdfFontFactory.CreateFont("Helvetica", PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED); } - return PdfFontFactory.CreateFont("Helvetica", PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED); } @@ -780,41 +775,6 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b GXLogging.Debug(log, "GxDrawText leftAux: " + leftAux + ",leftMargin:" + leftMargin + ",pageSize.Top:" + pageSize.GetTop() + ",bottomAux:" + bottomAux + ",topMargin:" + topMargin + ",bottomMargin:" + bottomMargin); if (htmlformat == 1) { - ConverterProperties properties = new ConverterProperties(); - if (IsTrueType(this.baseFont)) - { - Hashtable locations = GetFontLocations(); - foreach (string fontName in locations.Keys) - { - string fontPath = (string)locations[fontName]; - if (string.IsNullOrEmpty(fontPath)) - { - MSPDFFontDescriptor fontDescriptor = new MSPDFFontDescriptor(); - fontPath = fontDescriptor.getTrueTypeFontLocation(fontName); - } - if (!string.IsNullOrEmpty(fontPath)) - { - FontProvider fontProvider = new DefaultFontProvider(); - - if (IsEmbeddedFont(fontName)) - fontProvider.AddFont(fontPath, PdfEncodings.IDENTITY_H); - else - fontProvider.AddFont(fontPath, PdfEncodings.WINANSI); - - properties.SetFontProvider(fontProvider); - } - } - } - - //Bottom and top are the absolutes, regardless of the actual height at which the letters are written. - bottomAux = (float)convertScale(bottom); - topAux = (float)convertScale(top); - - Rectangle htmlRectangle = new Rectangle(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin, rightAux - leftAux, bottomAux - topAux); - Canvas cb = new Canvas(canvas, htmlRectangle); - TextAlignment colAlignment = (TextAlignment)GetTextAlignment(alignment); - cb.SetTextAlignment(colAlignment); - try { bottomAux = (float)convertScale(bottom); @@ -825,15 +785,19 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b float lly = drawingPageHeight - bottomAux; float urx = rightAux + leftMargin; float ury = drawingPageHeight - topAux; - + + 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); + ConverterProperties converterProperties = new ConverterProperties(); - converterProperties.SetFontProvider(document.GetFontProvider()); + converterProperties.SetFontProvider(new DefaultFontProvider()); //Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly - IList elements = HtmlConverter.ConvertToElements(sTxt, properties); + IList elements = HtmlConverter.ConvertToElements(sTxt, new ConverterProperties()); foreach (IElement element in elements) - processHTMLElement(cb, colAlignment, htmlRectangle, yPosition, (IBlockElement)element); + ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element); } catch (Exception ex1) { @@ -975,18 +939,18 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b } } - private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement) + private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement) { 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(cb, colAlignment, htmlRectangle, currentYPosition, (IBlockElement)child); + ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child); } - float blockElementHeight = getBlockElementHeight(blockElement, htmlRectangle); + float blockElementHeight = GetBlockElementHeight(blockElement, htmlRectangle); float availableSpace = currentYPosition.CurrentYPosition - htmlRectangle.GetBottom(); if (blockElementHeight > availableSpace) { @@ -1011,7 +975,6 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle float numWidth = new Paragraph("1. ").CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight(); list.SetFixedPosition(this.getPage(), htmlRectangle.GetX() + numWidth, currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); - list.SetTextAlignment(colAlignment); document.Add(list); currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; return; @@ -1021,8 +984,7 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle if (table != null) { table.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); - table.SetTextAlignment(colAlignment); - cb.Add(table); + document.Add(table); currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; return; } @@ -1040,7 +1002,6 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle if (image != null) { image.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); - image.SetTextAlignment(colAlignment); document.Add(image); currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; return; @@ -1048,14 +1009,14 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle } - private float getBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle) + private float GetBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle) { return blockElement.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight(); } + //Utility class used to know where the cursor is left after each block element (HTML tag) is rendered private class YPosition { - //Utility class used to know where the cursor is left after each block element (HTML tag) is rendered public YPosition(float initialYPosition) { CurrentYPosition = initialYPosition; @@ -1162,7 +1123,6 @@ public override bool IsSplitCharacter(GlyphLine text, int glyphPos) { myResult = true; } - return myResult || baseResult; } } @@ -1206,10 +1166,9 @@ public override void GxEndDocument() Canvas canvas = new Canvas(page, templateRectangle); canvas.ShowTextAligned(i.ToString(CultureInfo.InvariantCulture), templatex, templatey, TextAlignment.CENTER).SetBackgroundColor(templateColorFill).SetFont(templateFont).SetFontSize(templateFontSize); } - } - int copies = 1; + int copies = 1; try { copies = Convert.ToInt32(printerSettings.getProperty(form, Const.COPIES)); @@ -1305,10 +1264,7 @@ public override void GxEndDocument() } document.Close(); - - GXLogging.Debug(log, "GxEndDocument!"); - try { props.save(); @@ -1328,22 +1284,15 @@ public override void GxEndDocument() { outputStream.Close(); GXLogging.Debug(log, "GxEndDocument OUTPUT_SCREEN outputstream length" + outputStream.ToString().Length); - } catch (IOException e) { - ; - GXLogging.Error(log, "GxEndDocument OUTPUT_SCREEN error", e); - } try { showReport(docName, modal); } - catch (Exception) - { - - } - + catch (Exception){} break; + case Const.OUTPUT_PRINTER: try { outputStream.Close(); } catch (IOException) {; } // Cierro el archivo @@ -1354,11 +1303,9 @@ public override void GxEndDocument() printReport(docName, this.printerOutputMode == 0, printerSettings.getProperty(form, Const.PRINTER)); } } - catch (Exception) - { - - } + catch (Exception){} break; + case Const.OUTPUT_FILE: try { @@ -1368,19 +1315,16 @@ public override void GxEndDocument() catch (IOException e) { GXLogging.Error(log, "GxEndDocument OUTPUT_FILE error", e); - - ; } break; + case Const.OUTPUT_STREAM: case Const.OUTPUT_STREAM_PRINTER: - default: break; } outputStream = null; GXLogging.Debug(log, "GxEndDocument End"); - } public override void GxStartPage() { @@ -1401,6 +1345,5 @@ private bool IsTrueType(PdfFont font) return font.GetFontProgram() is TrueTypeFont; } } - }