Convert an image to an accessible PDF
Valid PDF/A documents with A-level (Accessibility) conformance (PDF/A-1a, PDF/A-2a, PDF/A-3a) must have text that can be reliably searched and copied. The content structure in the accessible PDF/A document can be accessed by technologies such as screen readers.
Download the full sample now in C# and Java.
Interested in C or other language samples? Let us know on the contact page and we'll add it to our samples backlog.
When converting an image to a PDF/A document with A-level conformance, descriptive text must be provided for each page that is added to the document. For example, "This color image shows a small sailing boat at sunset". The language of the text must also be provided in ISO 3166:2013 format. For example, "en" or "de-CH".
For scanned text documents, the Conversion Service can be used to recognize the characters in the documents (OCR) and tag the image with the recognized structure and text.
For more background about converting images to PDF documents, see Convert an image to a PDF document.
Steps to convert a document
- Reading the input image
- Selecting a conversion profile
- Specifying the conformance level
- Adding accessible content
- Converting the image to a PDF document
- Full example
You need to initialize the library.
Reading the input image
First you need to read the image you want to convert. To do this, load the input image from the file system into a read-only image Document
.
This Document
can then later be passed to the Converter
.
- .NET
- Java
// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.image.Document inDoc = com.pdftools.image.Document.open(inStr);
Selecting a conversion profile
You start the image conversion process by creating a conversion Profile
object.
The Profile
object defines the parameters that are applied to the conversion process.
This example uses the Archive
profile, which is intended for document workflows that convert images to PDF/A documents for archiving.
- .NET
- Java
/// Create the profile that defines the conversion parameters.
// The Archive profile converts images to PDF/A documents for archiving.
var profile = new Profiles.Archive();
// Create the profile that defines the conversion parameters.
// The Archive profile converts images to PDF/A documents for archiving.
Archive profile = new Archive();
Specifying the conformance level
By default, the Archive
profile produces output PDF documents with PDF/A-2b conformance.
If the document workflow requires accessible content to be included in the output PDF, then conformance Level A (Accessible) should be used instead.
This examples sets the document conformance to PDF/A-2a.
- .NET
- Java
// Set conformance of output document to PDF/A-2a
profile.Conformance = new Conformance(2, Conformance.PdfALevel.A);
// Set conformance of output document to PDF/A-2a
profile.setConformance(new Conformance(new Conformance.PdfAVersion(2, Level.A)));
Adding accessible content
For each page created in the output PDF/A document, alternate text must be added that represents the contents of the image on that page. In addition, the language of the text should be specified at a document level.
- .NET
- Java
// For PDF/A level 2a, an alternate text is required for each page of the image.
// This is optional for other PDF/A levels, e.g. PDF/A-2b.
profile.Language = "en";
profile.AlternateText.Add(alternateText);
// For PDF/A level A, an alternate text is required for each page of the image.
// This is optional for other PDF/A levels, e.g. PDF/A-2b.
profile.setLanguage("en");
profile.getAlternateText().add(alternateText);
Converting the image to a PDF document
After setting the appropriate Profile
and Conformance
levels, and adding accessible content, the final step is to instantiate a Converter
object and call its Convert
method.
The output PDF/A document is created at the specified path.
- .NET
- Java
// Create output stream
using var outStr = File.Create(outPath);
// Convert the image to a tagged PDF/A document
using var outDoc = new Converter().Convert(inDoc, outStr, profile);
// Create output stream
FileStream outStream = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Convert the image to a tagged PDF/A document
com.pdftools.pdf.Document outDoc = new Converter().convert(inDoc, outStream, profile));
Full example
- .NET
- Java
// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Create the profile that defines the conversion parameters.
// The Archive profile converts images to PDF/A documents for archiving.
var profile = new Profiles.Archive();
// Set conformance of output document to PDF/A-2a
profile.Conformance = new Conformance(2, Conformance.PdfALevel.A);
// For PDF/A level 2a, an alternate text is required for each page of the image.
// This is optional for other PDF/A levels, e.g. PDF/A-2b.
profile.Language = "en";
profile.AlternateText.Add(alternateText);
// Create output stream
using var outStr = File.Create(outPath);
// Convert the image to a tagged PDF/A document
using var outDoc = new Converter().Convert(inDoc, outStr, profile);
// Create the profile that defines the conversion parameters.
// The Archive profile converts images to PDF/A documents for archiving.
Archive profile = new Archive();
// Set conformance of output document to PDF/A-2a
profile.setConformance(new Conformance(new Conformance.PdfAVersion(2, Level.A)));
// For PDF/A level A, an alternate text is required for each page of the image.
// This is optional for other PDF/A levels, e.g. PDF/A-2b.
profile.setLanguage("en");
profile.getAlternateText().add(alternateText);
// Optionally, other profile parameters can be changed according to the
// requirements of your conversion process.
try (
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.image.Document inDoc = com.pdftools.image.Document.open(inStr);
// Create output stream
FileStream outStream = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Convert the image to a tagged PDF/A document
com.pdftools.pdf.Document outDoc = new Converter().convert(inDoc, outStream, profile))
{
}