C#實現創建標簽PDF文件的示例代碼

關於“標簽PDF文件(Tagged PDF)

標簽PDF文件包含描述文檔結構和各種文檔元素順序的元數據,是一種包含後端提供的可訪問標記,管理閱讀順序和文檔內容表示的邏輯結構的PDF文件[1]。

關於“標簽(Tag)

PDF標簽是通過屏幕閱讀器等支持技術訪問PDF文檔內容的關鍵。PDF標記在層次結構或標記樹(tag tree)中排列PDF內容[1]。

這裡的標簽是一種不可見的標簽,它提供關於PDF文檔內容的重要信息。帶標簽的PDF包含許多不同類型的標簽,但最常用的是文本、替代文本(圖像的替代文本)、標題、鏈接和鏈接描述[2]。

PDF標簽的用處及意義

添加PDF標簽不會改變文檔的視覺外觀,但它提供瞭一個不可見的層,用於格式化文檔與屏幕閱讀器協作工作,這就使得從PDF文件中提取文本和圖形變得更容易,並幫助屏幕閱讀器以正確的順序顯示文件內容。[2]

PDF標簽還可以用於將內容傳輸到屏幕較小的設備,如智能手機和平板電腦。[2]

如何創建標簽PDF文件

本文將要介紹的創建方法是以後端C#程序代碼的方式來創建標簽PDF文件。創建時,通過NuGet安裝引用PDF API-Spire.PDF for .NET,調用其提供的類及相關方法來標記內容、結構元素等。

C#

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interchange.TaggedPdf;
using System.Drawing;

namespace CreateTaggedPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建PdfDocument類的對象
            PdfDocument pdf = new PdfDocument();

            //添加一頁
            pdf.Pages.Add(PdfPageSize.A4);

            //設置tab order
            pdf.Pages[0].SetTabOrder(TabOrder.Structure);

            //創建PdfTaggedContent類的對象
            PdfTaggedContent taggedContent = new PdfTaggedContent(pdf);
            taggedContent.SetLanguage("en-US");
            taggedContent.SetTitle("test");

            //創建字體、畫刷、字符串格式
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 10), true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            //添加elements
            PdfStructureElement article = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document);
            PdfStructureElement paragraph1 = article.AppendChildElement(PdfStandardStructTypes.Paragraph);
            PdfStructureElement span1 = paragraph1.AppendChildElement(PdfStandardStructTypes.Span);
            span1.BeginMarkedContent(pdf.Pages[0]);
            //繪制內容到頁面
            pdf.Pages[0].Canvas.DrawString("A PDF tag is the key to accessing the contents of PDF documents with supporting technologies such as screen readers. ", font, brush, new Rectangle(40, 0, 480, 80), format);
            span1.EndMarkedContent(pdf.Pages[0]);

            PdfStructureElement paragraph2 = article.AppendChildElement(PdfStandardStructTypes.Paragraph);
            paragraph2.BeginMarkedContent(pdf.Pages[0]);
            pdf.Pages[0].Canvas.DrawString("A PDF tag arranges the PDF content in a hierarchical architecture or tag tree.", font, brush, new Rectangle(40, 80, 480, 80), format);
            paragraph2.EndMarkedContent(pdf.Pages[0]);

            PdfStructureElement figure1 = article.AppendChildElement(PdfStandardStructTypes.Figure);
            //Set Alternate text 
            figure1.Alt = "replacement text1";
            figure1.BeginMarkedContent(pdf.Pages[0], null);
            PdfImage image = PdfImage.FromFile(@"logo.png");
            pdf.Pages[0].Canvas.DrawImage(image, new PointF(40, 200), new SizeF(100, 100));//繪制圖片到頁面
            figure1.EndMarkedContent(pdf.Pages[0]);

            PdfStructureElement figure2 = article.AppendChildElement(PdfStandardStructTypes.Figure);
            //Set Alternate text
            figure2.Alt = "replacement text2";
            figure2.BeginMarkedContent(pdf.Pages[0], null);
            pdf.Pages[0].Canvas.DrawRectangle(PdfPens.Black, new Rectangle(300, 200, 100, 100));
            figure2.EndMarkedContent(pdf.Pages[0]);

            //保存文檔          
            pdf.SaveToFile("CreateTaggedFile_result.pdf");
        }
    }
}

vb.net

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interchange.TaggedPdf
Imports System.Drawing

Namespace CreateTaggedPDF
    Class Program
        Private Shared Sub Main(args As String())
            '創建PdfDocument類的對象
            Dim pdf As New PdfDocument()

            '添加一頁
            pdf.Pages.Add(PdfPageSize.A4)

            '設置tab order
            pdf.Pages(0).SetTabOrder(TabOrder.[Structure])

            '創建PdfTaggedContent類的對象
            Dim taggedContent As New PdfTaggedContent(pdf)
            taggedContent.SetLanguage("en-US")
            taggedContent.SetTitle("test")

            '創建字體、畫刷、字符串格式
            Dim font As New PdfTrueTypeFont(New Font("Times New Roman", 10), True)
            Dim brush As New PdfSolidBrush(Color.Black)
            Dim format As New PdfStringFormat(PdfTextAlignment.Left)

            '添加elements
            Dim article As PdfStructureElement = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document)
            Dim paragraph1 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Paragraph)
            Dim span1 As PdfStructureElement = paragraph1.AppendChildElement(PdfStandardStructTypes.Span)
            span1.BeginMarkedContent(pdf.Pages(0))
            '繪制內容到頁面
            pdf.Pages(0).Canvas.DrawString("A PDF tag is the key to accessing the contents of PDF documents with supporting technologies such as screen readers. ", font, brush, New Rectangle(40, 0, 480, 80), format)
            span1.EndMarkedContent(pdf.Pages(0))

            Dim paragraph2 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Paragraph)
            paragraph2.BeginMarkedContent(pdf.Pages(0))
            pdf.Pages(0).Canvas.DrawString("A PDF tag arranges the PDF content in a hierarchical architecture or tag tree.", font, brush, New Rectangle(40, 80, 480, 80), format)
            paragraph2.EndMarkedContent(pdf.Pages(0))

            Dim figure1 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Figure)
            'Set Alternate text 
            figure1.Alt = "replacement text1"
            figure1.BeginMarkedContent(pdf.Pages(0), Nothing)
            Dim image As PdfImage = PdfImage.FromFile("logo.png")
            pdf.Pages(0).Canvas.DrawImage(image, New PointF(40, 200), New SizeF(100, 100))
            '繪制圖片到頁面
            figure1.EndMarkedContent(pdf.Pages(0))

            Dim figure2 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Figure)
            'Set Alternate text
            figure2.Alt = "replacement text2"
            figure2.BeginMarkedContent(pdf.Pages(0), Nothing)
            pdf.Pages(0).Canvas.DrawRectangle(PdfPens.Black, New Rectangle(300, 200, 100, 100))
            figure2.EndMarkedContent(pdf.Pages(0))

            '保存文檔          
            pdf.SaveToFile("CreateTaggedFile_result.pdf")
            System.Diagnostics.Process.Start("CreateTaggedFile_result.pdf")
        End Sub
    End Class
End Namespace

參考資料:

[1]. https://247accessibledocuments.com/what-is-a-tagged-pdf/

[2]. https://accessibility-i.org/what-is-a-tagged-pdf/

到此這篇關於C#實現創建標簽PDF文件的示例代碼的文章就介紹到這瞭,更多相關C#創建標簽PDF內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: