Unofficial Content

This project provides additional functionalities to work with PDF files (.NET and Java generators for GeneXus 9.0 or higher). To download this project go to GeneXus Marketplace

Features

  • Add password (ModifyPermissions)
  • Digitally sign a document (AddSignature)
  • Concatenate several pdf files (ConcatFiles)
  • Modify permissions (printing and allow copying text) (ModifyPermissions)
  • Add a watermark (AddWatermark)
  • Get Text (GetText) (2)
  • PDF pages count (PageCount)(1)
  • Load fields (SetFields)(1)
  • Generate a PDF from a Tiff file(TiffToPdf)(1)
  • Add a text to a PDF(3)
  • Convert Html in a PDF (Html2PDF)(4)

Installation

Check the detail here

Use cases

Concatenate PDFs

 // Concat Files EXAMPLE
 &files.Add("…DocIn01.pdf") // &files is a character Collection variable
 &files.Add("…DocIn02.pdf")
 &files.Add("…DocIn03.pdf")
 &res = &PDFTools.ConcatFiles(&files,"…DocOut.pdf") // &PDFTools is PDFTools type (External Object)
 msg(&res)

Notice that if an incoming Pdf file is signed, the resulting PDF is not signed and will need to be signed again (for the complete content).

Modify PDF permissions

 // Modify Permissions EXAMPLE
 &permisson.Add(PDFpermission.AllowCopy) // &permisson is a numeric Collection
 &permisson.Add(PDFpermission.AllowPrinting)
 &res = &PDFTools.ModifyPermissions("…DocIn.pdf","…DocOut.pdf","pass",&permisson)  // &PDFTools is PDFTools type (External Object)
 msg(&res)

Add a digital certificate to the document (sign PDF)

 // Modify Add Signature EXAMPLE
&res = &PDFTools.AddSignature("…DocIn.pdf","…DocOut.pdf","…CertificatePath","pass",&IsVisible)
// Certificate in PFX format only – Password only if required
// isVisible = true -> show a mark in the PDF  

Get PDF text

Verify that you have the PDFManager External Object.

&PDFText = ""
&PDFText = &PDFManager.GetText(&PDFPath)
if (&PDFManager.Error)
  msg(format("Type:%1 Dsc:%2",&PDFManager.ErrorType,&PDFManager.ErrorDescription))
endif

Add a watermark

Verify that you have the PDFAddWatermark procedure.

PDFAddWatermark.Call(&PathSource,&PathTarget,&image,20,20)

where &PathSource references the origin file, &PathTarget the destination file, &image the image taken as a reference to the watermark.

When using the PDFAddWatermark option add the following to the C# compiler options

/r:bin\itextsharp.dll

Count PDF pages

&res = &PDFTools.PageCount(&PathSource)
msg(format("PageCount:%1",&res),status)

Fill PDF fields

It is important to emphasize that the PDF using as input has to be generated with a tool that has already defined the controls in the Form. The SetFields method simply assigns values to controls by name. Examples of how to generate a PDF with controls can be seen here and here.

&AttId = !"CustomerNameField"
&AttValue = !"Joe Doe"
&properties.Set(&AttId.Trim(),&AttValue.Trim()) // &properties is based on GX Properties data type
&res = ""
&res = &PDFTools.SetFields(&SourcePDFPath,&TargetPDFPath,&properties)
if not (&res.IsEmpty())
  msg(format("Error:%1",&res))
endif

Generate a PDF from a Tiff

&files.Add("C:\temp\sample1.tif")
&files.Add("C:\temp\sample2.tif")
&res = &PDFTools.TiffToPdf(&files,"C:\temp\sample3.pdf")
msg(format("Result:%1",&res),status)

Add a text in a specific position of a PDF

&result = &PDFTools.AddText(&PDFIn, &PDFOut, &X, &Y, &PageNumber, &Text, &fontSize)

Html2PDF

Based on a Variable containing Html code, it will generate a PDF file. You can use it as follows:

&result = &PDFTools.Html2PDF(&Content, &TargetPath, &FontPath)

where:

  • &Content details the Html code.
  • &TargetPath defies a full path location where the PDF will be generated.
  • &FontPath defines a full path for a font location, such as "c:/windows/fonts/l_10646.ttf", if not specified the Arial Unicode font is selected ("c:/windows/fonts/ARIALUNI.TTF").
  • &result defines possible error codes, empty if there is no error.

Considerations in Java version X

To be able to sign a pdf you must use a file with the pfx format, the following link details how to generate it http://www.tech-pro.net/export-to-pfx.html

When executing, the following error may occur in the document's signature:

java.io.IOException: exception unwrapping private key - java.security.InvalidKeyException: Illegal key size

It is necessary to update the security of the Virtual Machine Java as detailed here.

Download the jar that corresponds to "Unlimited Strength Jurisdiction Policy Files" from http://java.sun.com/javase/downloads/index.jsp in the section "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" and apply it.

Troubleshooting

The invoked member is not supported in a dynamic assembly

In some cases the following error can occur when using the C# generator:

Type:Exception Dsc:The invoked member is not supported in a dynamic assembly

change the machine.config file with the following:

<configuration>
    <runtime>
       <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>

You can find machine.config here.

32-bit
%windir%\Microsoft.NET\Framework\[version]\config\machine.config
64-bit
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config

NoClassDefFoundError: org.bouncycastle.cms.CMSException

In some cases the following error can occur when using the getText method in Java:

java.lang.NoClassDefFoundError: org.bouncycastle.cms.CMSException
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:169)
  at org.pdfbox.pdmodel.encryption.SecurityHandlersManager.class$(SecurityHandlersManager.java:84)

Download the following .jar from bcmail-jdk15-140.jar and copy it to the lib directory of the WebApp.

Type:Exception Dsc:null when on GetText function

If using the GetText function the error is detailed:

 Type:Exception Dsc:null

Replace the PDFBox-0.7.3.jar file with the following file: http://www.apache.org/dyn/closer.cgi/pdfbox/1.7.0/pdfbox-app-1.7.0.jar

'SdtPDFManager' could not be found in C#

Compiling a C# project the following error occurs:

error CS0246: The type or namespace name 'SdtPDFManager' could not be found (are you missing a using directive or an assembly reference?)

Make sure the PDFManager.dll file is located on the target Environment Directory \bin folder.

'PDFTextHandler' cannot find symbol

Compiling a Java project the following error occurs:

SdtPDFManager.java:120: error: cannot find symbol
   public pdftools.PDFTextHandler getExternalInstance( )
                  ^
  symbol:   class PDFTextHandler
  location: package pdftools

Make sure the pdfwrapper.jar and dependencies (Font*.jar, PDFBox*.jar) files are located on the target Environment Directory \web\drivers\ folder.

The type or namespace name 'lowagie' does not exist in the namespace 'com'

Compiling the PDFAddWatermark the following error is detailed:

error CS0234: The type or namespace name 'lowagie' does not exist in the namespace 'com' (are you missing an assembly reference?)

Make sure to update the PDFAddWatermark object to the latest version, use version 2.3.4 or higher (for example PDFTools_2.3.4_AddWatermark.xpz file)

error: package com.lowagie.text.pdf does not exist in Java

Compiling in a Java environment the following error occurs:

com\proyecto\pdfaddwatermark.java:86: error: package com.lowagie.text.pdf does not exist
com.lowagie.text.pdf.PdfReader reader = new com.lowagie.text.pdf.PdfReader(AV12PathSource);

Add the iText.jar to the Classpath property.

Type:Exception Dsc:No se admite el miembro invocado en un ensamblado dinámico

Using GeneXus v16 upgrade #9 and the C# generator with IIS 32 bit enabled the following error appears

Type:Exception Dsc:No se admite el miembro invocado en un ensamblado dinámico

Modify the machine.config file for the 32 bits framework, add the followint entry within the <runtime> tag

<loadFromRemoteSources enabled="true" />

Could not load file or assembly itextsharp Version=5.5.8.0

Since GeneXus 17 upgrade 8 the iTextsharp.dll was updated from 5.5.8.0 to 4.1.6.0 because of the license agreement (more information on SAC#50700). Use the PDFTools_2.3.7.1_Csharp.zip marketplace download which solves the case otherwise the following error will appear:

[FileLoadException: Could not load file or assembly 'itextsharp, Version=5.5.8.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
   PDFTools_iTextSharpLib.iTextSharpUtil.ModifyPermissions(String PathSource, String PathTarget, String UserPassword, List`1 Permissons) in iTextSharpUtil.cs:301
   GeneXus.Programs.SdtPDFTools.modifypermissions(String gxTp_PathSource, String gxTp_PathTarget, String gxTp_UserPassword, GxSimpleCollection`1 gxTp_Permissons) +124

Source Code

https://github.com/ladrians/PDFTools_iTextSharpLib C# implementation
https://github.com/ladrians/PDFTools_iTextJavaLib Java implementation

Considerations

NetCore generator is not supported, the following error appears

Could not load file or assembly 'itextsharp, Version=5.5.13.1, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'. The system cannot find the file specified.

(1) - Available since PDF Tools 2.2

(2) - Available since PDF Tools 2.3.2

(3) - Available since PDF Tools 2.3.4

(4) - Available since PDF Tools 2.3.6

Changelog

2.3.0 version

Since Xev2u3 itext* dependencies for C# where updated in GeneXus forcing the regenerate the wrapper DLL.

  • up to xev2u2 itextsharp.dll 4.0.2.0
  • since xev2u3 itextsharp.dll 5.3.5.0

Use PDFTools_2.3.0_CsharpDependencies_xev2u3.xpz for Xev2u3 or higher versions and PDFTools_2.3.0_CsharpDependencies_xev2u2.xpz for Xev2u2 and lower versions. The error detailed:

Could not load file or assembly 'itextsharp, Version=4.0.2.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. 
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

2.3.3 version

AddSignature method added to declare the rectangle and page number.

2.3.4 version

AddText method added to insert a text in a specific PDF page and position.

2.3.5 version - Valid for GeneXus 15.

Another AddText method to insert a text in a specific PDF page and position (angle and RGB).

2.3.6 version - Tested with GeneXus 16.

Add Html2PDF new method.

2.3.7 version - Solve error with Java and GeneXus 16 Upgrade #9

The following error appears when using version 2.3.6 and GeneXus 16 Upgrade #9 with Java

javax.servlet.ServletException: java.lang.NoSuchMethodError: org.bouncycastle.util.encoders.Hex.toHexString([B)Ljava/lang/String;
...
java.lang.IllegalStateException: java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\PDFtoolsU9JavaSQLServer\WEB-INF\lib\bcprov-jdk15on-1.60.jar (The system cannot find the file specified)
...
java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\PDFtoolsU9JavaSQLServer\WEB-INF\lib\bcprov-jdk15on-1.60.jar (The system cannot find the file specified)

To solve the issue

  • Import the PDFTools_2.3.7_JavaDependencies.xpz
  • Remove the files bcpkix-jdk15on-1.60.jar, bcprov-jdk15on-1.60.jar or bcprov-ext-jdk14-145.jarbcprov-jdk14-145.jar (this depends on the previous PDF tools version and GX used) from
    • The Knowledge Base (File resources)
    • \target environment folder\web\drivers
    • \tomcat web application\WEB-INF\lib
  • Since GeneXus 16 Upgrade #9 the valid files must be bcprov-jdk15on-1.64.jar and bcpkix-jdk15on-1.64.jar or higher.

Recompile the objects using the external object.

2.3.7.1 C# version - Compilation error in C# because of mishatch itextsharp.dll version

Download the PDFTools_2.3.7.1_Csharp.zip when using GeneXus 17 Upgrade #8 or higher with C#.

Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant