Skip to content

Commit e06243f

Browse files
Register font provider for HTML elements.
Rename PDFReportItext7 to PDFReportItext8
1 parent 114dc35 commit e06243f

File tree

7 files changed

+421
-16
lines changed

7 files changed

+421
-16
lines changed

dotnet/DotNetStandardClasses.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GXAzureEventGrid", "src\dot
249249
EndProject
250250
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreAttackMitigationTest", "test\DotNetCoreAttackMitigationTest\DotNetCoreAttackMitigationTest.csproj", "{2D615969-53E2-4B77-9A9A-75C33865CF76}"
251251
EndProject
252+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetPDFUnitTest", "test\DotNetPdfTest\DotNetPDFUnitTest.csproj", "{0FCFB078-5584-469F-92CC-61B0A6216D0D}"
253+
EndProject
252254
Global
253255
GlobalSection(SolutionConfigurationPlatforms) = preSolution
254256
Debug|Any CPU = Debug|Any CPU
@@ -603,6 +605,10 @@ Global
603605
{2D615969-53E2-4B77-9A9A-75C33865CF76}.Debug|Any CPU.Build.0 = Debug|Any CPU
604606
{2D615969-53E2-4B77-9A9A-75C33865CF76}.Release|Any CPU.ActiveCfg = Release|Any CPU
605607
{2D615969-53E2-4B77-9A9A-75C33865CF76}.Release|Any CPU.Build.0 = Release|Any CPU
608+
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
609+
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
610+
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
611+
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.Build.0 = Release|Any CPU
606612
EndGlobalSection
607613
GlobalSection(SolutionProperties) = preSolution
608614
HideSolutionNode = FALSE
@@ -722,6 +728,7 @@ Global
722728
{5BBC75F0-E51A-4EBD-A628-92498D319B1D} = {4C43F2DA-59E5-46F5-B691-195449498555}
723729
{7250CDB1-95C4-4822-B01B-3CBD73324CC9} = {30159B0F-BE61-4DB7-AC02-02851426BE4B}
724730
{2D615969-53E2-4B77-9A9A-75C33865CF76} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
731+
{0FCFB078-5584-469F-92CC-61B0A6216D0D} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
725732
EndGlobalSection
726733
GlobalSection(ExtensibilityGlobals) = postSolution
727734
SolutionGuid = {E18684C9-7D76-45CD-BF24-E3944B7F174C}

dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext7.cs renamed to dotnet/src/dotnetcore/GxPdfReportsCS/PDFReportItext8.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@
2626
using iText.Layout.Properties;
2727
using iText.Layout.Splitting;
2828
using log4net;
29-
using NetTopologySuite.Utilities;
30-
using static iText.Kernel.Pdf.Colorspace.PdfPattern;
3129
using Path = System.IO.Path;
32-
using Text = iText.Layout.Element.Text;
3330

3431
namespace GeneXus.Printer
3532
{
3633

37-
public class GxReportBuilderPdf7 : GxReportBuilderPdf
34+
public class GxReportBuilderPdf8 : GxReportBuilderPdf
3835
{
3936
static ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
40-
public GxReportBuilderPdf7() { }
41-
public GxReportBuilderPdf7(string appPath, Stream outputStream)
37+
public GxReportBuilderPdf8() { }
38+
public GxReportBuilderPdf8(string appPath, Stream outputStream)
4239
{
4340

44-
_pdfReport = new com.genexus.reports.PDFReportItextSharp7(appPath);
41+
_pdfReport = new com.genexus.reports.PDFReportItext8(appPath);
4542
if (outputStream != null)
4643
{
4744
_pdfReport.setOutputStream(outputStream);
@@ -54,7 +51,7 @@ public GxReportBuilderPdf7(string appPath, Stream outputStream)
5451
namespace com.genexus.reports
5552
{
5653

57-
public class PDFReportItextSharp7 : PDFReportItextBase
54+
public class PDFReportItext8 : PDFReportItextBase
5855
{
5956

6057
static ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
@@ -78,7 +75,7 @@ public class PDFReportItextSharp7 : PDFReportItextBase
7875
private Boolean fontBold;
7976
private Boolean fontItalic;
8077

81-
public PDFReportItextSharp7(String appPath) : base(appPath)
78+
public PDFReportItext8(String appPath) : base(appPath)
8279
{
8380
documentImages = new Dictionary<string, Image>();
8481
}
@@ -777,6 +774,28 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
777774
{
778775
try
779776
{
777+
ConverterProperties converterProperties = new ConverterProperties();
778+
FontProvider fontProvider = new DefaultFontProvider();
779+
if (IsTrueType(baseFont))
780+
{
781+
Hashtable locations = GetFontLocations();
782+
foreach (string fontName in locations.Keys)
783+
{
784+
string fontPath = (string)locations[fontName];
785+
if (string.IsNullOrEmpty(fontPath))
786+
{
787+
MSPDFFontDescriptor fontDescriptor = new MSPDFFontDescriptor();
788+
fontPath = fontDescriptor.getTrueTypeFontLocation(fontName);
789+
}
790+
if (!string.IsNullOrEmpty(fontPath))
791+
{
792+
793+
fontProvider.AddFont(fontPath);
794+
}
795+
}
796+
}
797+
document.SetFontProvider(fontProvider);
798+
converterProperties.SetFontProvider(fontProvider);
780799
bottomAux = (float)convertScale(bottom);
781800
topAux = (float)convertScale(top);
782801
float drawingPageHeight = this.pageSize.GetTop() - topMargin - bottomMargin;
@@ -792,12 +811,11 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
792811
PdfCanvas htmlPdfCanvas = new PdfCanvas(pdfPage);
793812
Canvas htmlCanvas = new Canvas(canvas, htmlRectangle);
794813

795-
ConverterProperties converterProperties = new ConverterProperties();
796-
converterProperties.SetFontProvider(new DefaultFontProvider());
814+
797815
//Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly
798-
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, new ConverterProperties());
816+
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, converterProperties);
799817
foreach (IElement element in elements)
800-
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element);
818+
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element, fontProvider);
801819
}
802820
catch (Exception ex1)
803821
{
@@ -939,14 +957,14 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
939957
}
940958
}
941959

942-
private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
960+
private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement, FontProvider fontProvider)
943961
{
944962
Div div = blockElement as Div;
945963
if (div != null) {
946964
// Iterate through the children of the Div and process each child element recursively
947965
foreach (IElement child in div.GetChildren())
948966
if (child is IBlockElement)
949-
ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child);
967+
ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child, fontProvider);
950968

951969
}
952970

dotnet/src/dotnetframework/GxClasses/Model/gxproc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static public IReportHandler GetPrinter( int outputType, string path, Stream rep
471471
Type classType = assem.GetType( "GeneXus.Printer.GxReportBuilderPdf", false, true);
472472
reportHandler = (IReportHandler) Activator.CreateInstance(classType,new Object[]{path, reportOutputStream});
473473
#else
474-
string reportBuidler = Preferences.PdfReportLibrary().Equals(PDF_LIBRARY_ITEXT8, StringComparison.OrdinalIgnoreCase) ? "GxReportBuilderPdf7" : "GxReportBuilderPdf";
474+
string reportBuidler = Preferences.PdfReportLibrary().Equals(PDF_LIBRARY_ITEXT8, StringComparison.OrdinalIgnoreCase) ? "GxReportBuilderPdf8" : "GxReportBuilderPdf";
475475
reportHandler = (IReportHandler)(ClassLoader.FindInstance("GxPdfReportsCS", "GeneXus.Printer", reportBuidler, new Object[] { path, reportOutputStream }, null));
476476
#endif
477477
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net6.0</TargetFrameworks>
4+
<NoWarn>CS8032;1701;1702;NU1701</NoWarn>
5+
<RollForward>Major</RollForward>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
8+
<DefineConstants>TRACE;NETCORE</DefineConstants>
9+
</PropertyGroup>
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
11+
<DefineConstants>DEBUG;TRACE;NETCORE</DefineConstants>
12+
</PropertyGroup>
13+
<ItemGroup>
14+
<None Remove="log.console.config" />
15+
</ItemGroup>
16+
17+
18+
19+
20+
<ItemGroup>
21+
<PackageReference Include="itext7" Version="8.0.0" />
22+
<PackageReference Include="itext7.font-asian" Version="8.0.0" />
23+
<PackageReference Include="itext7.pdfhtml" Version="5.0.0" />
24+
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.0" />
25+
<PackageReference Include="iTextSharp-LGPL" Version="4.1.6" />
26+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
27+
<PackageReference Include="xunit" Version="2.4.1" />
28+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
<PrivateAssets>all</PrivateAssets>
31+
</PackageReference>
32+
<PackageReference Include="coverlet.collector" Version="3.0.2">
33+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
34+
<PrivateAssets>all</PrivateAssets>
35+
</PackageReference>
36+
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<ProjectReference Include="..\..\src\dotnetcore\GxClasses\GxClasses.csproj" />
41+
<ProjectReference Include="..\..\src\dotnetcore\GxPdfReportsCS\GxPdfReportsCS.csproj" />
42+
</ItemGroup>
43+
44+
<ItemGroup>
45+
<None Update="appsettings.json">
46+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
</None>
48+
</ItemGroup>
49+
50+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.IO;
3+
using GeneXus.Configuration;
4+
using GeneXus.Programs;
5+
using GeneXus.Utils;
6+
using Xunit;
7+
8+
namespace UnitTesting
9+
{
10+
public class PDFTests
11+
{
12+
[Fact]
13+
public void TestITextFormat()
14+
{
15+
string report = "PDFFormat.pdf";
16+
if (File.Exists(report))
17+
File.Delete(report);
18+
try
19+
{
20+
apdfformat test = new apdfformat();
21+
test.execute();
22+
}
23+
#pragma warning disable CA1031 // Do not catch general exception types
24+
catch (Exception ex)
25+
#pragma warning restore CA1031 // Do not catch general exception types
26+
{
27+
Console.WriteLine(ex.ToString());
28+
}
29+
Assert.True(File.Exists(report));
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)