How to add alternative text to images in a Tagged PDF using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add alternate text to images in a PDF document using C# and VB.NET.
Steps to draw image on PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics
- Use the following code snippet to create the PDF and draw an image with alternate text.
C#
//Creates new PDF document PdfDocument doc = new PdfDocument(); //Set the document title doc.DocumentInformation.Title = "Image"; //Creates new page PdfPage page = doc.Pages.Add(); //Draw string page.Graphics.DrawString("JPEG Image:", new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold), PdfBrushes.Blue, new PointF(0, 10)); //Create a new PDF bitmap object PdfBitmap bitmap = new PdfBitmap("sample.jpg"); //Set the tag type PdfStructureElement imageElement = new PdfStructureElement(PdfTagType.Figure); //Set the alternate text imageElement.AlternateText = "Bridge"; //Adding tag to the PDF image bitmap.PdfTag = imageElement; //Draw image bitmap.Draw(page.Graphics, new PointF(50, 30)); //Save the document and dispose it doc.Save("Image.pdf"); doc.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Image.pdf");
VB.NET
'Creates new PDF document Dim doc As PdfDocument = New PdfDocument() 'Set the document title doc.DocumentInformation.Title = "Image" 'Creates new page Dim page As PdfPage = doc.Pages.Add() 'Draw string page.Graphics.DrawString("JPEG Image:", New PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold), PdfBrushes.Blue, New PointF(0, 10)) 'Create a new PDF bitmap object Dim bitmap As PdfBitmap = New PdfBitmap("sample.jpg") 'Set the tag type Dim imageElement As PdfStructureElement = New PdfStructureElement(PdfTagType.Figure) 'Set the alternate text imageElement.AlternateText = "Bridge" 'Adding tag to the PDF image bitmap.PdfTag = imageElement 'Draw image bitmap.Draw(page.Graphics, New PointF(50, 30)) 'Save the document and dispose it doc.Save("Image.pdf") doc.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Image.pdf")
A complete working sample can be downloaded from PdfTagAlternateText.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with tagged PDF, where you will find other options like adding tag to shapes, form fields, annotations, hyperlink, template, table, and auto tag PDF.
An online sample link demonstrating the use of tag in PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.