javafontsitext7pdfa

Itext7 error on embedded fonts when generating pdfa


I am trying to generate a PDF-A but I get the following error when I try to set the font to the Paragraph :

Exception in thread "main" com.itextpdf.pdfa.exceptions.PdfAConformanceException: All the fonts must be embedded. This one is not: Courier
    at com.itextpdf.pdfa.checker.PdfA1Checker.checkFont(PdfA1Checker.java:305)
    at com.itextpdf.pdfa.PdfADocument.flushFonts(PdfADocument.java:282)
    at com.itextpdf.kernel.pdf.PdfDocument.close(PdfDocument.java:968)
    at com.itextpdf.layout.Document.close(Document.java:117)
    at testcreatepdfa.TestCreatePDFA.main(TestCreatePDFA.java:62)
C:\Users\UC9001309\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

In the normal pdf generation I used these commands to set the font to the paragraph :

PdfFont font = PdfFontFactory.createFont(StandardFonts.COURIER);
Paragraph p = new Paragraph();
p.setFont(font);

But in PDF-A I get the error about embed fonts. Maybe because it is already incorporated? Does anyone know which command to use for p.setFont without creating the font but using the font embedded ?

package testcreatepdfa;

import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfOutputIntent;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.pdfa.PdfADocument;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author UC9001309
 */
public class TestCreatePDFA {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        PdfWriter   pdfWriter = new PdfWriter("C:\\Temp\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()) + ".pdf");
          PdfADocument pdfA = new PdfADocument( pdfWriter,
        PdfAConformanceLevel.PDF_A_1B,
          new PdfOutputIntent("Custom", "", "https://www.color.org",
            "sRGB2014", new FileInputStream("C:\\Users\\UC9001309\\Documents\\NetBeansProjects\\GestionePdf\\sRGB2014.icc")));       
        //Document document = new Document(pdfA); 
          
        Document document = new Document(pdfA, PageSize.A4, false);
        pdfA.setTagged();
        pdfA.getCatalog().setLang(new PdfString("it-IT"));
        pdfA.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
        PdfDocumentInfo info = pdfA.getDocumentInfo();
        info.setTitle("TEST PdfA "); 
        System.out.println(FontProgramFactory.getRegisteredFontFamilies());
        System.out.println(FontProgramFactory.getRegisteredFonts());

        PdfFont font = PdfFontFactory.createFont(StandardFonts.COURIER);

        Paragraph p = new Paragraph();
        p.setFont(font);
        p.add("Prova pdfa");
        document.add(p);
        document.close();
    }
}

Thanks!


Solution

  • Solution adopted to solve the problem:

    import com.itextpdf.kernel.font.PdfFont;
    import com.itextpdf.kernel.font.PdfFontFactory;
    import com.itextpdf.kernel.geom.PageSize;
    import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
    import com.itextpdf.kernel.pdf.PdfDocumentInfo;
    import com.itextpdf.kernel.pdf.PdfOutputIntent;
    import com.itextpdf.kernel.pdf.PdfString;
    import com.itextpdf.kernel.pdf.PdfViewerPreferences;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.itextpdf.layout.Document;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.pdfa.PdfADocument;
    import com.itextpdf.pdfa.checker.PdfAChecker;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    
    
    /**
     *
     * @author UC9001309
     */
    public class TestCreatePDFA {
    
    public static final String courier = "C:\\Windows\\fonts\\couri.ttf";
    public static final String times = "C:\\Windows\\fonts\\times.ttf";
    public static final String helvetica = "C:\\Windows\\fonts\\helvetica.ttf";
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            // TODO code application logic here
             PdfWriter   pdfWriter = new PdfWriter("C:\\Temp\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()) + ".pdf");
              PdfADocument pdfA = new PdfADocument( pdfWriter,PdfAConformanceLevel.PDF_A_1B,new PdfOutputIntent("Custom", "", "https://www.color.org",
                "sRGB2014", new FileInputStream("C:\\Windows\\System32\\spool\\drivers\\color\\sRGB Color Space Profile.icm")));  
                  
             
             PdfAChecker chk = null;
            
            Document document = new Document(pdfA, PageSize.A4, false);
            pdfA.setTagged();        
            pdfA.getCatalog().setLang(new PdfString("it-IT"));
            pdfA.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
            PdfDocumentInfo info = pdfA.getDocumentInfo();
            info.setTitle("TEST PdfA " ); 
            
            PdfFont font_h = PdfFontFactory.createFont(helvetica);
            PdfFont font_c = PdfFontFactory.createFont(courier);
            PdfFont font_t = PdfFontFactory.createFont(times);
            
             Paragraph p = new Paragraph();
             p.setFont(font_c);
             p.setItalic();
             p.add("Prova pdfa");
             
             Paragraph p1 = new Paragraph();
             p1.setFont(font_h);
          
             p1.add("Prova pdfa");
             
             Paragraph p2 = new Paragraph();
             p2.setFont(font_t);
          
             p2.add("Prova pdfa");
             
             document.add(p);
             document.add(p1);
             document.add(p2);
             document.close();
        }
        
    }