Question

I’ve recently started evaluating ITextSharp 5.4.5 for use, and have read the IText in Action se book which has covered a large portion of the library. It’s easy to use, and I’ve seen an uptick in use over the last year by the community, but I’ve run into an issue that I’m hoping someone can address.

How can I set an ATL to an image of a document I’m creating from scratch?

Approach 1 (Create the image and set the Alt property):

    private static void Main(string[] args)
    {
        using (Document doc = new Document(PageSize.A4, 36, 36, 36, 36))
        {
            using (PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream("c:\\ImageTest.pdf", FileMode.Create)))
            {
                pw.SetTagged();
                pw.UserProperties = true;
                doc.Open();
                Image img = Image.GetInstance(@"c:\images\WA.png");
                img.Alt = "Alt text for Image!";
                doc.Add(img);
                doc.Close();
            }
        }
    }

Approach 2 (Create the image and call SetAccessibleAttribute):

    private static void Main(string[] args)
    {
        using (Document doc = new Document(PageSize.A4, 36, 36, 36, 36))
        {
            using (PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream("c:\\ImageTest.pdf", FileMode.Create)))
            {
                pw.SetTagged();
                pw.UserProperties = true;
                doc.Open();
                Image img = Image.GetInstance(@"c:\images\WA.png");
                img.SetAccessibleAttribute(PdfName.ALT, new PdfString("Alt text for Image!"));
                doc.Add(img);
                doc.Close();
            }
        }
    }

Approach 3 (Create the image and use DirectContent.AddImage):

    private static void Main(string[] args)
    {
        using (Document doc = new Document(PageSize.A4, 36, 36, 36, 36))
        {
            using (PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream("c:\\ImageTest.pdf", FileMode.Create)))
            {
                pw.SetTagged();
                pw.UserProperties = true;
                doc.Open();
                PdfDictionary dict = new PdfDictionary();
                dict.Put(PdfName.LANG, new PdfString("en-us"));
                dict.Put(PdfName.ALT, new PdfString("Alt text for Image!"));
                pw.DirectContent.BeginMarkedContentSequence(PdfName.FIGURE, dict, true);
                Image img = Image.GetInstance(@"c:\images\WA.png");
                img.SetAbsolutePosition(36, 592);
                pw.DirectContent.AddImage(img);
                pw.DirectContent.EndMarkedContentSequence();
                pw.Flush();
                doc.Close();
            }
        }
    }

Still no Alt Text is rendered, any guidance would be appreciated.

Thanks.

Was it helpful?

Solution

It’s not entirely obvious, but upon further research I discovered that accessibility attributes will not be written unless the pdf version is 1.7 or above. The following source renders the ALT value as expected.

    private static void Main(string[] args)
    {
        using (Document doc = new Document(PageSize.A4, 36, 36, 36, 36))
        {
            using (PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream("c:\\ImageTest.pdf", FileMode.Create)))
            {
                pw.SetTagged();
                pw.UserProperties = true;
                doc.Open();
                pw.PdfVersion = PdfWriter.VERSION_1_7;
                Image img = Image.GetInstance(@"c:\images\WA.png");
                img.SetAbsolutePosition(36, 592);
                img.Alt = "Alt Text for Image!";
                doc.Add(img);
                doc.Close();
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top