Alternative Text for Images in Existing PDF Documents | |
SelectPdf PDF Library for .NET can fix the alternative text of the images in an existing tagged PDF document: add a description where one is missing, replace a bad one (a file name, "image1"), or mark an image that is only decoration as decorative. Missing alternative text is the most common reason a PDF fails PDF/UA (ISO 14289-1, checkpoint 7.3), and this turns such a file into a passing one without generating it again.
The work is done with PdfAccessibilityManager:
Load - loads the tagged PDF document, password protected or not, from a file, Stream, byte array or PdfDocument object.
Figures - the figures of the document, in reading order: its Figure and Formula elements, including custom element types the document maps to them.
MissingAlternateText - the figures that still have no description: neither alternative nor actual text, or only a placeholder such as a file name ("chart1.png"), a numbered name ("image1", "Picture 3") or a GUID. Decorative figures are not listed.
Save - saves the document with the changes to a file, Stream or byte array, and GetDocument returns it as a PdfDocument object, to continue working on it.
Each PdfFigure says where it is and what describes it:
PageIndex and Bounds - the page it starts on and its box on it, in points from the top-left corner of the page as displayed.
Type - Figure, or Formula, which needs a description too.
AlternateText - the description a screen reader reads in place of the image.
ActualText - the text the figure shows, when it is a picture of text: a formula, a scanned word, a logo with a name in it.
IsDecorative - set it for an image that carries no information (a border, a background flourish). When saved, the figure leaves the structure tree and its content is marked as an artifact: the page looks the same, and screen readers skip it.
GetImage - renders the figure's area of its page as a PNG image (150 dpi by default), to show a person who writes the description, or to send to a service that describes images.
Setting a text to null or an empty string removes it.
Describe every figure that has no description, and mark a decorative one:
using (PdfAccessibilityManager manager = new PdfAccessibilityManager()) { manager.Load("report.pdf"); foreach (PdfFigure figure in manager.MissingAlternateText) { // show the image to a person, or to an image-description service byte[] png = figure.GetImage(150); figure.AlternateText = Describe(png); } manager.Figures[3].IsDecorative = true; // a border: screen readers skip it manager.Save("report-fixed.pdf"); }
Tagged documents only. An untagged document has no structure element to carry a description, and alternative text alone would not make it accessible, so Load refuses it with a PdfDocumentException that says so. To make new documents accessible from the start, see Accessible PDF and PDF/A.
Setting a text writes the element's /Alt (or /ActualText) entry and nothing else; the pages are not changed.
A signed document is appended to, so its signatures still verify after the change.
A document that declares PDF/UA is checked again when it is saved: a change that would leave some of its content outside the structure tree is refused.
Documents the library creates with Tagged write an image's AlternateText as its description, and an image given none gets no description (version 26.6 and later; earlier versions wrote a GUID in its place). Such a figure is listed by MissingAlternateText.