Skip to main content
Skip table of contents

PDF/A-3


pdfa3

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.pdfa;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
import com.itextpdf.kernel.pdf.PdfOutputIntent;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDate;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.pdfa.PdfADocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.StringTokenizer;
import java.io.InputStream;

public class PdfA3 {
    public static final String DEST = "./target/sandbox/pdfa/pdf_a3.pdf";

    public static final String BOLD = "./src/main/resources/font/OpenSans-Bold.ttf";

    public static final String DATA = "./src/main/resources/data/united_states.csv";

    public static final String FONT = "./src/main/resources/font/OpenSans-Regular.ttf";


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

        new PdfA3().manipulatePdf(DEST);
    }

    public void manipulatePdf(String dest) throws IOException {
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
        PdfFont bold = PdfFontFactory.createFont(BOLD, PdfEncodings.IDENTITY_H);

        InputStream inputStream = new FileInputStream("./src/main/resources/data/sRGB_CS_profile.icm");
        PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_3B,
                new PdfOutputIntent("Custom", "",
                        null, "sRGB IEC61966-2.1", inputStream));

        Document document = new Document(pdfDoc, PageSize.A4.rotate());

        PdfDictionary parameters = new PdfDictionary();
        parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());

        // Embeds file to the document
        PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc, Files.readAllBytes(Paths.get(DATA)),
                "united_states.csv", "united_states.csv", new PdfName("text/csv"), parameters, PdfName.Data);
        pdfDoc.addAssociatedFile("united_states.csv", fileSpec);

        Table table = new Table(UnitValue.createPercentArray(
                new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1})).useAllAvailableWidth();

        try (BufferedReader br = new BufferedReader(new FileReader(DATA))) {

            // Reads content of csv file
            String line = br.readLine();

            process(table, line, bold, 10, true);
            while ((line = br.readLine()) != null) {
                process(table, line, font, 10, false);
            }
        }

        document.add(table);
        document.close();
    }

    public void process(Table table, String line, PdfFont font, int fontSize, boolean isHeader) {

        // Parses csv string line with specified delimiter
        StringTokenizer tokenizer = new StringTokenizer(line, ";");

        while (tokenizer.hasMoreTokens()) {
            Paragraph content = new Paragraph(tokenizer.nextToken()).setFont(font).setFontSize(fontSize);

            if (isHeader) {
                table.addHeaderCell(content);
            } else {
                table.addCell(content);
            }
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Commons.Utils;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Filespec;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Pdfa;

namespace iText.Samples.Sandbox.Pdfa
{
    public class PdfA3
    {
        public static readonly string DEST = "results/sandbox/pdfa/pdf_a3.pdf";

        public static readonly String BOLD = "../../../resources/font/OpenSans-Bold.ttf";

        public static readonly String DATA = "../../../resources/data/united_states.csv";

        public static readonly String FONT = "../../../resources/font/OpenSans-Regular.ttf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new PdfA3().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(String dest)
        {
            PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            PdfFont bold = PdfFontFactory.CreateFont(BOLD, PdfEncodings.IDENTITY_H);

            Stream fileStream =
                new FileStream("../../../resources/data/sRGB_CS_profile.icm", FileMode.Open, FileAccess.Read);
            PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_3B,
                new PdfOutputIntent("Custom", "",
                    null, "sRGB IEC61966-2.1", fileStream));

            Document document = new Document(pdfDoc, PageSize.A4.Rotate());

            PdfDictionary parameters = new PdfDictionary();
            parameters.Put(PdfName.ModDate, new PdfDate().GetPdfObject());
            
            // Embeds file to the document 
            PdfFileSpec fileSpec = PdfFileSpec.CreateEmbeddedFileSpec(pdfDoc, 
                File.ReadAllBytes(DATA), 
                "united_states.csv", "united_states.csv",
                new PdfName("text/csv"), parameters, PdfName.Data);
            pdfDoc.AddAssociatedFile("united_states.csv", fileSpec);

            Table table = new Table(UnitValue.CreatePercentArray(
                new float[] {4, 1, 3, 4, 3, 3, 3, 3, 1})).UseAllAvailableWidth();

            using (StreamReader streamReader = new StreamReader(DATA))
            {
                // Reads content of csv file
                String line = streamReader.ReadLine();

                Process(table, line, bold, 10, true);
                while ((line = streamReader.ReadLine()) != null)
                {
                    Process(table, line, font, 10, false);
                }

                streamReader.Close();
            }

            document.Add(table);
            document.Close();
        }

        public virtual void Process(Table table, String line, PdfFont font, int fontSize, bool isHeader)
        {
            // Parses csv string line with specified delimiter
            StringTokenizer tokenizer = new StringTokenizer(line, ";");

            while (tokenizer.HasMoreTokens())
            {
                Paragraph content = new Paragraph(tokenizer.NextToken()).SetFont(font).SetFontSize(fontSize);

                if (isHeader)
                {
                    table.AddHeaderCell(content);
                }
                else
                {
                    table.AddCell(content);
                }
            }
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.