C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件的示例代碼
關(guān)于“標(biāo)簽PDF文件(Tagged PDF)
標(biāo)簽PDF文件包含描述文檔結(jié)構(gòu)和各種文檔元素順序的元數(shù)據(jù),是一種包含后端提供的可訪問標(biāo)記,管理閱讀順序和文檔內(nèi)容表示的邏輯結(jié)構(gòu)的PDF文件[1]。
關(guān)于“標(biāo)簽(Tag)
PDF標(biāo)簽是通過屏幕閱讀器等支持技術(shù)訪問PDF文檔內(nèi)容的關(guān)鍵。PDF標(biāo)記在層次結(jié)構(gòu)或標(biāo)記樹(tag tree)中排列PDF內(nèi)容[1]。
這里的標(biāo)簽是一種不可見的標(biāo)簽,它提供關(guān)于PDF文檔內(nèi)容的重要信息。帶標(biāo)簽的PDF包含許多不同類型的標(biāo)簽,但最常用的是文本、替代文本(圖像的替代文本)、標(biāo)題、鏈接和鏈接描述[2]。
PDF標(biāo)簽的用處及意義
添加PDF標(biāo)簽不會(huì)改變文檔的視覺外觀,但它提供了一個(gè)不可見的層,用于格式化文檔與屏幕閱讀器協(xié)作工作,這就使得從PDF文件中提取文本和圖形變得更容易,并幫助屏幕閱讀器以正確的順序顯示文件內(nèi)容。[2]
PDF標(biāo)簽還可以用于將內(nèi)容傳輸?shù)狡聊惠^小的設(shè)備,如智能手機(jī)和平板電腦。[2]
如何創(chuàng)建標(biāo)簽PDF文件
本文將要介紹的創(chuàng)建方法是以后端C#程序代碼的方式來創(chuàng)建標(biāo)簽PDF文件。創(chuàng)建時(shí),通過NuGet安裝引用PDF API-Spire.PDF for .NET,調(diào)用其提供的類及相關(guān)方法來標(biāo)記內(nèi)容、結(jié)構(gòu)元素等。
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)
{
//創(chuàng)建PdfDocument類的對(duì)象
PdfDocument pdf = new PdfDocument();
//添加一頁(yè)
pdf.Pages.Add(PdfPageSize.A4);
//設(shè)置tab order
pdf.Pages[0].SetTabOrder(TabOrder.Structure);
//創(chuàng)建PdfTaggedContent類的對(duì)象
PdfTaggedContent taggedContent = new PdfTaggedContent(pdf);
taggedContent.SetLanguage("en-US");
taggedContent.SetTitle("test");
//創(chuàng)建字體、畫刷、字符串格式
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]);
//繪制內(nèi)容到頁(yè)面
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));//繪制圖片到頁(yè)面
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())
'創(chuàng)建PdfDocument類的對(duì)象
Dim pdf As New PdfDocument()
'添加一頁(yè)
pdf.Pages.Add(PdfPageSize.A4)
'設(shè)置tab order
pdf.Pages(0).SetTabOrder(TabOrder.[Structure])
'創(chuàng)建PdfTaggedContent類的對(duì)象
Dim taggedContent As New PdfTaggedContent(pdf)
taggedContent.SetLanguage("en-US")
taggedContent.SetTitle("test")
'創(chuàng)建字體、畫刷、字符串格式
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))
'繪制內(nèi)容到頁(yè)面
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))
'繪制圖片到頁(yè)面
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/
到此這篇關(guān)于C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件的示例代碼的文章就介紹到這了,更多相關(guān)C#創(chuàng)建標(biāo)簽PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#打包部署并把.net framework框架打到安裝包的方法步驟
打包c(diǎn)#程序時(shí),有時(shí)需要添加.net framework組件到安裝包,本文就來介紹一下C#打包部署并把.net framework框架打到安裝包的方法步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C# 創(chuàng)建文本文件寫入讀取實(shí)現(xiàn)代碼
C# 創(chuàng)建文本文件寫入讀取,可以用來做系統(tǒng)日志或程序操作日志或者錯(cuò)誤記錄,需要的朋友可以參考下。2011-11-11
C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫(kù)建模的基本教程
這篇文章主要介紹了C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫(kù)建模的基本教程,LINQ to SQL被集成在.NET框架之中,需要的朋友可以參考下2016-03-03

