In This Topic
Programming / PDF / Creating a tagged PDF document from scratch

Creating a tagged PDF document from scratch

In This Topic

It is not so complicated to add tags to your PDF document. However, it is tricky to attach them correctly to create a valid tagged PDF document.

This small piece of code demonstrates how to create a very simple PDF document, that is properly tagged. This document contains two images and some text and you can denote it the fully accessible PDF document.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.
Using oGdPicturePDF As GdPicturePDF = New GdPicturePDF()
    'Creating a new PDF document.
    If (oGdPicturePDF.NewPDF(PdfConformance.PDF_UA_1) = GdPictureStatus.OK) AndAlso
       (oGdPicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) Then
        'This is required to have a valid PDF_UA document.
        oGdPicturePDF.SetTitle("My first PDF/UA document")
        'Adding the font for drawing a text.
        Dim fontResName As String = oGdPicturePDF.AddTrueTypeFontU("Arial", False, False, True)
        oGdPicturePDF.SetTextSize(18)
        'Adding the first image to the PDF document.
        Dim logo1 As String = oGdPicturePDF.AddJpegImageFromFile("brand1.jpg")
        If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
            'Adding the second image to the PDF document.
            Dim logo2 As String = oGdPicturePDF.AddJpegImageFromFile("brand2.jpg")
            If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
                'Creating a root tag and set the language.
                Dim tagRootID As Integer = oGdPicturePDF.GetTagRootID()
                If (oGdPicturePDF.GetStat() <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.SetLanguage("en-US") <> GdPictureStatus.OK) Then GoTo [error]
                'Creating a section tag for the document content.
                Dim tagSection As Integer = oGdPicturePDF.NewTag(tagRootID, "Sect")
                If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then GoTo [error]
                'Creating a tag for the text of the sentence.
                If (oGdPicturePDF.BeginMarkedContentSequence(tagSection, "Span") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.DrawText(fontResName, 20, 750, "The product") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.EndMarkedContent() <> GdPictureStatus.OK) Then GoTo [error]
                'Creating a tag and setting its properties for the first image.
                Dim tagLogo1 As Integer = oGdPicturePDF.NewTag(tagSection, "Figure")
                If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then GoTo [error]
                If (oGdPicturePDF.SetTagAlternateDescription(tagLogo1, "This is a logo of brand1.") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.SetTagAttribute(tagLogo1, "BBox", New Double() {125, 750, 221.75, 771.75}) <> GdPictureStatus.OK) Then GoTo [error]
                'Creating a tag for the image itself and drawing the image.
                If (oGdPicturePDF.BeginMarkedContentSequence(tagLogo1, "Figure") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.DrawImage(logo1, 125, 750, 96.75F, 21.75F) <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.EndMarkedContent() <> GdPictureStatus.OK) Then GoTo [error]
                'Creating a tag for the text of the sentence.
                If (oGdPicturePDF.BeginMarkedContentSequence(tagSection, "Span") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.DrawText(fontResName, 230, 750, "is powered by") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.EndMarkedContent() <> GdPictureStatus.OK) Then GoTo [error]
                'Creating a tag and setting its properties for the second image.
                Dim tagLogo2 As Integer = oGdPicturePDF.NewTag(tagSection, "Figure")
                If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then GoTo [error]
                If (oGdPicturePDF.SetTagAlternateDescription(tagLogo2, "This is a logo of brand2.") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.SetTagAttribute(tagLogo2, "BBox", New Double() {350, 750, 446.75, 771.75}) <> GdPictureStatus.OK) Then GoTo [error]
                'Creating a tag for the image itself and drawing the image.
                If (oGdPicturePDF.BeginMarkedContentSequence(tagLogo2, "Figure") <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.DrawImage(logo2, 350, 750, 96.75F, 21.75F) <> GdPictureStatus.OK) OrElse
                   (oGdPicturePDF.EndMarkedContent() <> GdPictureStatus.OK) Then GoTo [error]
[error]:
                If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
                    'Saving the created document.
                    If oGdPicturePDF.SaveToFile("tagged_document.pdf") = GdPictureStatus.OK Then
                        MessageBox.Show("Your tagged PDF document has been successfully created.", "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        MessageBox.Show("The error occured when saving. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    MessageBox.Show("The error occured when creating tags. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("The error occured when adding the second image. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("The error occured when adding the first image. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        oGdPicturePDF.CloseDocument()
    Else
        MessageBox.Show("The new document can't be created. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Using
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.
using (GdPicturePDF oGdPicturePDF = new GdPicturePDF())
{
    //Creating a new PDF document.
    if ((oGdPicturePDF.NewPDF(PdfConformance.PDF_UA_1) == GdPictureStatus.OK) &&
        (oGdPicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK))
    {
        //This is required to have a valid PDF_UA document.
        oGdPicturePDF.SetTitle("My first PDF/UA document");
        // Add the font for drawing a text.
        string fontResName = oGdPicturePDF.AddTrueTypeFontU("Arial", false, false, true);
        oGdPicturePDF.SetTextSize(18);
        //Adding the first image to the PDF document.
        string logo1 = oGdPicturePDF.AddJpegImageFromFile("brand1.jpg");
        if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
        {
            //Adding the second image to the PDF document.
            string logo2 = oGdPicturePDF.AddJpegImageFromFile("brand2.jpg");
            if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
            {
                //Creating a root tag and set the language.
                int tagRootID = oGdPicturePDF.GetTagRootID();
                if ((oGdPicturePDF.GetStat() != GdPictureStatus.OK) ||
                    (oGdPicturePDF.SetLanguage("en-US") != GdPictureStatus.OK))
                    goto error;
                //Creating a section tag for the document content.
                int tagSection = oGdPicturePDF.NewTag(tagRootID, "Sect");
                if (oGdPicturePDF.GetStat() != GdPictureStatus.OK) goto error;
                //Creating a tag for the text of the sentence.
                if ((oGdPicturePDF.BeginMarkedContentSequence(tagSection, "Span") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.DrawText(fontResName, 20, 750, "The product") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.EndMarkedContent() != GdPictureStatus.OK))
                    goto error;
                //Creating a tag and setting its properties for the first image.
                int tagLogo1 = oGdPicturePDF.NewTag(tagSection, "Figure");
                if (oGdPicturePDF.GetStat() != GdPictureStatus.OK) goto error;
                if ((oGdPicturePDF.SetTagAlternateDescription(tagLogo1, "This is a logo of brand1.") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.SetTagAttribute(tagLogo1, "BBox", new double[] { 125, 750, 221.75, 771.75 }) != GdPictureStatus.OK))
                    goto error;
                //Creating a tag for the image itself and drawing the image.
                if ((oGdPicturePDF.BeginMarkedContentSequence(tagLogo1, "Figure") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.DrawImage(logo1, 125, 750, 96.75f, 21.75f) != GdPictureStatus.OK) ||
                    (oGdPicturePDF.EndMarkedContent() != GdPictureStatus.OK))
                    goto error;
                //Creating a tag for the text of the sentence.
                if ((oGdPicturePDF.BeginMarkedContentSequence(tagSection, "Span") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.DrawText(fontResName, 230, 750, "is powered by") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.EndMarkedContent() != GdPictureStatus.OK))
                    goto error;
                //Creating a tag and setting its properties for the second image.
                int tagLogo2 = oGdPicturePDF.NewTag(tagSection, "Figure");
                if (oGdPicturePDF.GetStat() != GdPictureStatus.OK) goto error;
                if ((oGdPicturePDF.SetTagAlternateDescription(tagLogo2, "This is a logo of brand2.") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.SetTagAttribute(tagLogo2, "BBox", new double[] { 350, 750, 446.75, 771.75 }) != GdPictureStatus.OK))
                    goto error;
                //Creating a tag for the image itself and drawing the image.
                if ((oGdPicturePDF.BeginMarkedContentSequence(tagLogo2, "Figure") != GdPictureStatus.OK) ||
                    (oGdPicturePDF.DrawImage(logo2, 350, 750, 96.75f, 21.75f) != GdPictureStatus.OK) ||
                    (oGdPicturePDF.EndMarkedContent() != GdPictureStatus.OK))
                    goto error;
                error:
                if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
                {
                    //Saving the created document.
                    if (oGdPicturePDF.SaveToFile("tagged_document.pdf") == GdPictureStatus.OK)
                        MessageBox.Show("Your tagged PDF document has been successfully created.", "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("The error occured when saving. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    MessageBox.Show("The error occured when creating tags. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                MessageBox.Show("The error occured when adding the second image. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
            MessageBox.Show("The error occured when adding the first image. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        oGdPicturePDF.CloseDocument();
    }
    else
        MessageBox.Show("The new document can't be created. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating a Tagged PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}