Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:itext8' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiamurialdo authored and genexusbot committed Sep 25, 2023
1 parent f9fc2b3 commit b5f7b17
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 16 deletions.
7 changes: 7 additions & 0 deletions dotnet/DotNetStandardClasses.sln
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GXAzureEventGrid", "src\dot
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreAttackMitigationTest", "test\DotNetCoreAttackMitigationTest\DotNetCoreAttackMitigationTest.csproj", "{2D615969-53E2-4B77-9A9A-75C33865CF76}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetPDFUnitTest", "test\DotNetPdfTest\DotNetPDFUnitTest.csproj", "{0FCFB078-5584-469F-92CC-61B0A6216D0D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -613,6 +615,10 @@ Global
{2D615969-53E2-4B77-9A9A-75C33865CF76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D615969-53E2-4B77-9A9A-75C33865CF76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D615969-53E2-4B77-9A9A-75C33865CF76}.Release|Any CPU.Build.0 = Release|Any CPU
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -729,6 +735,7 @@ Global
{5BBC75F0-E51A-4EBD-A628-92498D319B1D} = {4C43F2DA-59E5-46F5-B691-195449498555}
{7250CDB1-95C4-4822-B01B-3CBD73324CC9} = {30159B0F-BE61-4DB7-AC02-02851426BE4B}
{2D615969-53E2-4B77-9A9A-75C33865CF76} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
{0FCFB078-5584-469F-92CC-61B0A6216D0D} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E18684C9-7D76-45CD-BF24-E3944B7F174C}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@
using iText.Layout.Properties;
using iText.Layout.Splitting;
using log4net;
using NetTopologySuite.Utilities;
using static iText.Kernel.Pdf.Colorspace.PdfPattern;
using Path = System.IO.Path;
using Text = iText.Layout.Element.Text;

namespace GeneXus.Printer
{

public class GxReportBuilderPdf7 : GxReportBuilderPdf
public class GxReportBuilderPdf8 : GxReportBuilderPdf
{
static ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public GxReportBuilderPdf7() { }
public GxReportBuilderPdf7(string appPath, Stream outputStream)
public GxReportBuilderPdf8() { }
public GxReportBuilderPdf8(string appPath, Stream outputStream)
{

_pdfReport = new com.genexus.reports.PDFReportItextSharp7(appPath);
_pdfReport = new com.genexus.reports.PDFReportItext8(appPath);
if (outputStream != null)
{
_pdfReport.setOutputStream(outputStream);
Expand All @@ -54,7 +51,7 @@ public GxReportBuilderPdf7(string appPath, Stream outputStream)
namespace com.genexus.reports
{

public class PDFReportItextSharp7 : PDFReportItextBase
public class PDFReportItext8 : PDFReportItextBase
{

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

public PDFReportItextSharp7(String appPath) : base(appPath)
public PDFReportItext8(String appPath) : base(appPath)
{
documentImages = new Dictionary<string, Image>();
}
Expand Down Expand Up @@ -777,6 +774,28 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
{
try
{
ConverterProperties converterProperties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
if (IsTrueType(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.AddFont(fontPath);
}
}
}
document.SetFontProvider(fontProvider);
converterProperties.SetFontProvider(fontProvider);
bottomAux = (float)convertScale(bottom);
topAux = (float)convertScale(top);
float drawingPageHeight = this.pageSize.GetTop() - topMargin - bottomMargin;
Expand All @@ -792,12 +811,11 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
PdfCanvas htmlPdfCanvas = new PdfCanvas(pdfPage);
Canvas htmlCanvas = new Canvas(canvas, htmlRectangle);

ConverterProperties converterProperties = new ConverterProperties();
converterProperties.SetFontProvider(new DefaultFontProvider());

//Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, new ConverterProperties());
IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, converterProperties);
foreach (IElement element in elements)
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element);
ProcessHTMLElement(htmlRectangle, yPosition, (IBlockElement)element, fontProvider);
}
catch (Exception ex1)
{
Expand Down Expand Up @@ -939,14 +957,14 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
}
}

private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement)
private void ProcessHTMLElement(Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement, FontProvider fontProvider)
{
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);
ProcessHTMLElement(htmlRectangle, currentYPosition, (IBlockElement)child, fontProvider);

}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/dotnetframework/GxClasses/Model/gxproc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static public IReportHandler GetPrinter( int outputType, string path, Stream rep
Type classType = assem.GetType( "GeneXus.Printer.GxReportBuilderPdf", false, true);
reportHandler = (IReportHandler) Activator.CreateInstance(classType,new Object[]{path, reportOutputStream});
#else
string reportBuidler = Preferences.PdfReportLibrary().Equals(PDF_LIBRARY_ITEXT8, StringComparison.OrdinalIgnoreCase) ? "GxReportBuilderPdf7" : "GxReportBuilderPdf";
string reportBuidler = Preferences.PdfReportLibrary().Equals(PDF_LIBRARY_ITEXT8, StringComparison.OrdinalIgnoreCase) ? "GxReportBuilderPdf8" : "GxReportBuilderPdf";
reportHandler = (IReportHandler)(ClassLoader.FindInstance("GxPdfReportsCS", "GeneXus.Printer", reportBuidler, new Object[] { path, reportOutputStream }, null));
#endif
}
Expand Down
50 changes: 50 additions & 0 deletions dotnet/test/DotNetPdfTest/DotNetPDFUnitTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<NoWarn>CS8032;1701;1702;NU1701</NoWarn>
<RollForward>Major</RollForward>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE;NETCORE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE;NETCORE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Remove="log.console.config" />
</ItemGroup>




<ItemGroup>
<PackageReference Include="itext7" Version="8.0.0" />
<PackageReference Include="itext7.font-asian" Version="8.0.0" />
<PackageReference Include="itext7.pdfhtml" Version="5.0.0" />
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.0" />
<PackageReference Include="iTextSharp-LGPL" Version="4.1.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\dotnetcore\GxClasses\GxClasses.csproj" />
<ProjectReference Include="..\..\src\dotnetcore\GxPdfReportsCS\GxPdfReportsCS.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions dotnet/test/DotNetPdfTest/PDFTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.IO;
using GeneXus.Configuration;
using GeneXus.Programs;
using GeneXus.Utils;
using Xunit;

namespace UnitTesting
{
public class PDFTests
{
[Fact]
public void TestITextFormat()
{
string report = "PDFFormat.pdf";
if (File.Exists(report))
File.Delete(report);
try
{
apdfformat test = new apdfformat();
test.execute();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
Console.WriteLine(ex.ToString());
}
Assert.True(File.Exists(report));
}
}
}
Loading

0 comments on commit b5f7b17

Please sign in to comment.