Creating a simple PDF/UA document
This example was written in answer to the question How can I generate a PDF/UA compatible PDF with iText?
pdfua
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.pdfua;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfVersion;
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.kernel.xmp.XMPException;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PdfUA {
public static final String DEST = "./target/sandbox/pdfua/pdf_ua.pdf";
public static final String DOG = "./src/main/resources/img/dog.bmp";
public static final String FONT = "./src/main/resources/font/FreeSans.ttf";
public static final String FOX = "./src/main/resources/img/fox.bmp";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new PdfUA().manipulatePdf(DEST);
}
public void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest,
new WriterProperties().addUAXmpMetadata().setPdfVersion(PdfVersion.PDF_1_7)));
Document document = new Document(pdfDoc, PageSize.A4.rotate());
//TAGGED PDF
//Make document tagged
pdfDoc.setTagged();
//PDF/UA
//Set document metadata
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
pdfDoc.getCatalog().setLang(new PdfString("en-US"));
PdfDocumentInfo info = pdfDoc.getDocumentInfo();
info.setTitle("English pangram");
Paragraph p = new Paragraph();
//PDF/UA
//Embed font
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
p.setFont(font);
p.add("The quick brown ");
Image img = new Image(ImageDataFactory.create(FOX));
//PDF/UA
//Set alt text
img.getAccessibilityProperties().setAlternateDescription("Fox");
p.add(img);
p.add(" jumps over the lazy ");
img = new Image(ImageDataFactory.create(DOG));
//PDF/UA
//Set alt text
img.getAccessibilityProperties().setAlternateDescription("Dog");
p.add(img);
document.add(p);
p = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n").setFont(font).setFontSize(20);
document.add(p);
List list = new List().setFont(font).setFontSize(20);
list.add(new ListItem("quick"));
list.add(new ListItem("brown"));
list.add(new ListItem("fox"));
list.add(new ListItem("jumps"));
list.add(new ListItem("over"));
list.add(new ListItem("the"));
list.add(new ListItem("lazy"));
list.add(new ListItem("dog"));
document.add(list);
document.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Pdfua
{
public class PdfUA
{
public static readonly string DEST = "results/sandbox/pdfua/pdf_ua.pdf";
public static readonly String DOG = "../../../resources/img/dog.bmp";
public static readonly String FONT = "../../../resources/font/FreeSans.ttf";
public static readonly String FOX = "../../../resources/img/fox.bmp";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new PdfUA().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest,
new WriterProperties().AddUAXmpMetadata().SetPdfVersion
(PdfVersion.PDF_1_7)));
Document document = new Document(pdfDoc, PageSize.A4.Rotate());
//TAGGED PDF
//Make document tagged
pdfDoc.SetTagged();
//PDF/UA
//Set document metadata
pdfDoc.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
PdfDocumentInfo info = pdfDoc.GetDocumentInfo();
info.SetTitle("English pangram");
Paragraph p = new Paragraph();
//PDF/UA
//Embed font
PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI,
PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
p.SetFont(font);
p.Add("The quick brown ");
Image img = new Image(ImageDataFactory.Create(FOX));
//PDF/UA
//Set alt text
img.GetAccessibilityProperties().SetAlternateDescription("Fox");
p.Add(img);
p.Add(" jumps over the lazy ");
img = new Image(ImageDataFactory.Create(DOG));
//PDF/UA
//Set alt text
img.GetAccessibilityProperties().SetAlternateDescription("Dog");
p.Add(img);
document.Add(p);
p = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n").SetFont(font).SetFontSize(20);
document.Add(p);
List list = new List().SetFont(font).SetFontSize(20);
list.Add(new ListItem("quick"));
list.Add(new ListItem("brown"));
list.Add(new ListItem("fox"));
list.Add(new ListItem("jumps"));
list.Add(new ListItem("over"));
list.Add(new ListItem("the"));
list.Add(new ListItem("lazy"));
list.Add(new ListItem("dog"));
document.Add(list);
document.Close();
}
}
}