0

I'm trying to run following example in my netbeans ide(HindiExample.java)

https://developers.itextpdf.com/examples/font-examples/clone-language-specific-examples

I have created maven project for library dependency following is my pom.xml

   <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.in.jagran</groupId>
    <artifactId>GenPdfApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
     <repositories>
    <repository>
        <id>itext</id>
        <name>iText Repository - releases</name>
        <url>https://repo.itextsupport.com/releases</url>
    </repository>
</repositories>
    <dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.18</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.0.4</version>
        <type>pom</type>
    </dependency>


  <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-licensekey</artifactId>
    <version>3.0.0</version>
  </dependency>
</dependencies>

</project>

but my project not found com.itextpdf.samples.GenericTest

so my question is what am i missing? what is the purpose of GenericTest Class?

Manish Pandey
  • 101
  • 3
  • 12

1 Answers1

1

You don't need GenericTest. That class is used for our own testing purposes. Remove all references to GenericTest and just execute the main method.

package com.itextpdf.samples.sandbox.fonts;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.BaseDirection;
import com.itextpdf.licensekey.LicenseKey;

import java.io.File;

public class ArabicExample {
    public static final String ARABIC
            = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
    public static final String DEST
            = "./target/test/resources/sandbox/fonts/arabic_example.pdf";
    public static final String FONT
            = "./src/test/resources/font/NotoNaskhArabic-Regular.ttf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ArabicExample().manipulatePdf(DEST);
    }

    @Override
    protected void manipulatePdf(String dest) throws Exception {
        //Load the license file to use advanced typography features
        LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-typography.xml");

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        PdfFont f = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);

        Paragraph p = new Paragraph("This is auto detection: ");
        p.add(new Text(ARABIC).setFont(f));
        p.add(new Text(": 50.00 USD"));
        doc.add(p);

        p = new Paragraph("This is correct manual property: ").setBaseDirection(BaseDirection.LEFT_TO_RIGHT).setFontScript(Character.UnicodeScript.ARABIC);
        p.add(new Text(ARABIC).setFont(f));
        p.add(new Text(": 50.00"));
        doc.add(p);

        doc.close();
    }
}

I removed the following stuff:

import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
@Category(SampleTest.class)

extends GenericTest

Now you can run the example as a standalone example, without the need for the testing infrastructure.

IMPORTANT: You jumped straight to the examples. Please also take a look at the pdfCalligraph web page. It contains a link to a white paper with plenty of interesting information: pdfCalligraph white paper

You might also want to read chapter 2 of the building blocks tutorial, because I see that you are missing a dependency:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>typography</artifactId>
    <version>1.0.0</version>
    <scope>compile</scope>
</dependency>

Without this dependency (that includes the pdfCalligraph) add-on, your code won't work.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • thanks for reply Bruno one more question what is System.getenv("ITEXT7_LICENSEKEY") in loadLicenseFile method.I'm passing license key directly and its work. LicenseKey.loadLicenseFile("D:/itextkey1522659193034_0.xml"); – Manish Pandey Apr 03 '18 at 08:26
  • I don't want to hard code the location of the license file into my applications. Instead I put the base path of the license key in the environment variables of my apps. The `System.getenv()` methods loads that path from the environment variables. – Bruno Lowagie Apr 03 '18 at 11:09