Skip to content

Commit 114dc35

Browse files
Code cleanup
1 parent 2796ee2 commit 114dc35

File tree

1 file changed

+20
-77
lines changed

1 file changed

+20
-77
lines changed

dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs

Lines changed: 20 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public GxReportBuilderPdf7(string appPath, Stream outputStream)
4444
_pdfReport = new com.genexus.reports.PDFReportItextSharp7(appPath);
4545
if (outputStream != null)
4646
{
47-
4847
_pdfReport.setOutputStream(outputStream);
4948
GXLogging.Debug(log, "GxReportBuilderPdf outputStream: binaryWriter");
5049
}
@@ -108,7 +107,6 @@ protected override void init(ref int gxYPage, ref int gxXPage, int pageWidth, in
108107
pdfDocument.SetDefaultPageSize(this.pageSize);
109108
document = new Document(pdfDocument);
110109

111-
112110
}
113111
catch (PdfException de)
114112
{
@@ -622,7 +620,6 @@ public override void GxAttris(String fontName, int fontSize, bool fontBold, bool
622620
break;
623621
}
624622
}
625-
626623
baseFont = PdfFontFactory.CreateFont(fontName, PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
627624
}
628625
}
@@ -634,7 +631,6 @@ public override void GxAttris(String fontName, int fontSize, bool fontBold, bool
634631
fontStyle = new Style();
635632
if (fontItalic) fontStyle.SetItalic();
636633
if (fontBold) fontStyle.SetBold();
637-
638634
}
639635
string fontPath = GetFontLocation(fontName);
640636
bool foundFont = true;
@@ -707,7 +703,6 @@ private PdfFont CreateDefaultFont()
707703
else
708704
defaultFont = PdfFontFactory.CreateFont("Helvetica", PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
709705
}
710-
711706
return PdfFontFactory.CreateFont("Helvetica", PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
712707
}
713708

@@ -780,41 +775,6 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
780775
GXLogging.Debug(log, "GxDrawText leftAux: " + leftAux + ",leftMargin:" + leftMargin + ",pageSize.Top:" + pageSize.GetTop() + ",bottomAux:" + bottomAux + ",topMargin:" + topMargin + ",bottomMargin:" + bottomMargin);
781776
if (htmlformat == 1)
782777
{
783-
ConverterProperties properties = new ConverterProperties();
784-
if (IsTrueType(this.baseFont))
785-
{
786-
Hashtable locations = GetFontLocations();
787-
foreach (string fontName in locations.Keys)
788-
{
789-
string fontPath = (string)locations[fontName];
790-
if (string.IsNullOrEmpty(fontPath))
791-
{
792-
MSPDFFontDescriptor fontDescriptor = new MSPDFFontDescriptor();
793-
fontPath = fontDescriptor.getTrueTypeFontLocation(fontName);
794-
}
795-
if (!string.IsNullOrEmpty(fontPath))
796-
{
797-
FontProvider fontProvider = new DefaultFontProvider();
798-
799-
if (IsEmbeddedFont(fontName))
800-
fontProvider.AddFont(fontPath, PdfEncodings.IDENTITY_H);
801-
else
802-
fontProvider.AddFont(fontPath, PdfEncodings.WINANSI);
803-
804-
properties.SetFontProvider(fontProvider);
805-
}
806-
}
807-
}
808-
809-
//Bottom and top are the absolutes, regardless of the actual height at which the letters are written.
810-
bottomAux = (float)convertScale(bottom);
811-
topAux = (float)convertScale(top);
812-
813-
Rectangle htmlRectangle = new Rectangle(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin, rightAux - leftAux, bottomAux - topAux);
814-
Canvas cb = new Canvas(canvas, htmlRectangle);
815-
TextAlignment colAlignment = (TextAlignment)GetTextAlignment(alignment);
816-
cb.SetTextAlignment(colAlignment);
817-
818778
try
819779
{
820780
bottomAux = (float)convertScale(bottom);
@@ -825,15 +785,19 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
825785
float lly = drawingPageHeight - bottomAux;
826786
float urx = rightAux + leftMargin;
827787
float ury = drawingPageHeight - topAux;
828-
788+
789+
Rectangle htmlRectangle = new Rectangle(llx, lly, urx - llx, ury - lly);
829790
YPosition yPosition = new YPosition(htmlRectangle.GetTop());
830791

792+
PdfCanvas htmlPdfCanvas = new PdfCanvas(pdfPage);
793+
Canvas htmlCanvas = new Canvas(canvas, htmlRectangle);
794+
831795
ConverterProperties converterProperties = new ConverterProperties();
832-
converterProperties.SetFontProvider(document.GetFontProvider());
796+
converterProperties.SetFontProvider(new DefaultFontProvider());
833797
//Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly
834-
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, properties);
798+
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, new ConverterProperties());
835799
foreach (IElement element in elements)
836-
processHTMLElement(cb, colAlignment, htmlRectangle, yPosition, (IBlockElement)element);
800+
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element);
837801
}
838802
catch (Exception ex1)
839803
{
@@ -975,18 +939,18 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
975939
}
976940
}
977941

978-
private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
942+
private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
979943
{
980944
Div div = blockElement as Div;
981945
if (div != null) {
982946
// Iterate through the children of the Div and process each child element recursively
983947
foreach (IElement child in div.GetChildren())
984948
if (child is IBlockElement)
985-
processHTMLElement(cb, colAlignment, htmlRectangle, currentYPosition, (IBlockElement)child);
949+
ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child);
986950

987951
}
988952

989-
float blockElementHeight = getBlockElementHeight(blockElement, htmlRectangle);
953+
float blockElementHeight = GetBlockElementHeight(blockElement, htmlRectangle);
990954
float availableSpace = currentYPosition.CurrentYPosition - htmlRectangle.GetBottom();
991955
if (blockElementHeight > availableSpace)
992956
{
@@ -1011,7 +975,6 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle
1011975
float numWidth = new Paragraph("1. ").CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight();
1012976
list.SetFixedPosition(this.getPage(), htmlRectangle.GetX() + numWidth, currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1013977

1014-
list.SetTextAlignment(colAlignment);
1015978
document.Add(list);
1016979
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1017980
return;
@@ -1021,8 +984,7 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle
1021984
if (table != null)
1022985
{
1023986
table.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1024-
table.SetTextAlignment(colAlignment);
1025-
cb.Add(table);
987+
document.Add(table);
1026988
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
1027989
return;
1028990
}
@@ -1040,22 +1002,21 @@ private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle
10401002
if (image != null)
10411003
{
10421004
image.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth());
1043-
image.SetTextAlignment(colAlignment);
10441005
document.Add(image);
10451006
currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight;
10461007
return;
10471008
}
10481009

10491010
}
10501011

1051-
private float getBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle)
1012+
private float GetBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle)
10521013
{
10531014
return blockElement.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight();
10541015
}
10551016

1017+
//Utility class used to know where the cursor is left after each block element (HTML tag) is rendered
10561018
private class YPosition
10571019
{
1058-
//Utility class used to know where the cursor is left after each block element (HTML tag) is rendered
10591020
public YPosition(float initialYPosition)
10601021
{
10611022
CurrentYPosition = initialYPosition;
@@ -1162,7 +1123,6 @@ public override bool IsSplitCharacter(GlyphLine text, int glyphPos)
11621123
{
11631124
myResult = true;
11641125
}
1165-
11661126
return myResult || baseResult;
11671127
}
11681128
}
@@ -1206,10 +1166,9 @@ public override void GxEndDocument()
12061166
Canvas canvas = new Canvas(page, templateRectangle);
12071167
canvas.ShowTextAligned(i.ToString(CultureInfo.InvariantCulture), templatex, templatey, TextAlignment.CENTER).SetBackgroundColor(templateColorFill).SetFont(templateFont).SetFontSize(templateFontSize);
12081168
}
1209-
12101169
}
1211-
int copies = 1;
12121170

1171+
int copies = 1;
12131172
try
12141173
{
12151174
copies = Convert.ToInt32(printerSettings.getProperty(form, Const.COPIES));
@@ -1305,10 +1264,7 @@ public override void GxEndDocument()
13051264

13061265
}
13071266
document.Close();
1308-
1309-
13101267
GXLogging.Debug(log, "GxEndDocument!");
1311-
13121268
try
13131269
{
13141270
props.save();
@@ -1328,22 +1284,15 @@ public override void GxEndDocument()
13281284
{
13291285
outputStream.Close();
13301286
GXLogging.Debug(log, "GxEndDocument OUTPUT_SCREEN outputstream length" + outputStream.ToString().Length);
1331-
13321287
}
13331288
catch (IOException e)
13341289
{
1335-
;
1336-
13371290
GXLogging.Error(log, "GxEndDocument OUTPUT_SCREEN error", e);
1338-
13391291
}
13401292
try { showReport(docName, modal); }
1341-
catch (Exception)
1342-
{
1343-
1344-
}
1345-
1293+
catch (Exception){}
13461294
break;
1295+
13471296
case Const.OUTPUT_PRINTER:
13481297
try { outputStream.Close(); }
13491298
catch (IOException) {; } // Cierro el archivo
@@ -1354,11 +1303,9 @@ public override void GxEndDocument()
13541303
printReport(docName, this.printerOutputMode == 0, printerSettings.getProperty(form, Const.PRINTER));
13551304
}
13561305
}
1357-
catch (Exception)
1358-
{
1359-
1360-
}
1306+
catch (Exception){}
13611307
break;
1308+
13621309
case Const.OUTPUT_FILE:
13631310
try
13641311
{
@@ -1368,19 +1315,16 @@ public override void GxEndDocument()
13681315
catch (IOException e)
13691316
{
13701317
GXLogging.Error(log, "GxEndDocument OUTPUT_FILE error", e);
1371-
1372-
;
13731318
}
13741319
break;
1320+
13751321
case Const.OUTPUT_STREAM:
13761322
case Const.OUTPUT_STREAM_PRINTER:
1377-
13781323
default: break;
13791324
}
13801325
outputStream = null;
13811326

13821327
GXLogging.Debug(log, "GxEndDocument End");
1383-
13841328
}
13851329
public override void GxStartPage()
13861330
{
@@ -1401,6 +1345,5 @@ private bool IsTrueType(PdfFont font)
14011345
return font.GetFontProgram() is TrueTypeFont;
14021346
}
14031347
}
1404-
14051348
}
14061349

0 commit comments

Comments
 (0)