翻譯|行業(yè)資訊|編輯:胡濤|2024-08-26 10:47:42.293|閱讀 90 次
概述:本文介紹如何使用Spire.PDF for .NET在 C# 和 VB.NET 中從頭開始創(chuàng)建帶標(biāo)簽的 PDF 。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
帶標(biāo)簽的 PDF(也稱為 PDF/UA)是一種包含底層標(biāo)簽樹(類似于 HTML)的 PDF,用于定義文檔的結(jié)構(gòu)。這些標(biāo)簽可以幫助屏幕閱讀器瀏覽整個文檔而不會丟失任何信息。本文介紹如何使用Spire.PDF for .NET在 C# 和 VB.NET 中從頭開始創(chuàng)建帶標(biāo)簽的 PDF 。
Spire.PDF for .NET 是一款獨(dú)立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發(fā)人員可以新建一個 PDF 文檔或者對現(xiàn)有的 PDF 文檔進(jìn)行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
首先,您需要將 Spire.PDF for.NET 包中包含的 DLL 文件作為引用添加到您的 .NET 項(xiàng)目中。
PM> Install-Package Spire.PDF
要在帶標(biāo)簽的 PDF 文檔中添加結(jié)構(gòu)元素,我們必須首先創(chuàng)建PdfTaggedContent類的對象。然后,使用PdfTaggedContent.StructureTreeRoot.AppendChildElement()方法將元素添加到根。以下是使用 Spire.PDF for .NET 向帶標(biāo)簽的 PDF 添加“標(biāo)題”元素的詳細(xì)步驟。
以下代碼片段提供了一個示例,說明如何在 C# 和 VB.NET 中在標(biāo)記的 PDF 文檔中創(chuàng)建各種元素,包括文檔、標(biāo)題、段落、圖形和表格。
【C#】
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interchange.TaggedPdf;
using Spire.Pdf.Tables;
using System.Data;
using System.Drawing;
namespace CreatePDFUA
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(20));
//Set tab order
page.SetTabOrder(TabOrder.Structure);
//Create an object of PdfTaggedContent class
PdfTaggedContent taggedContent = new PdfTaggedContent(doc);
//Set language and title for the document
taggedContent.SetLanguage("en-US");
taggedContent.SetTitle("test");
//Set PDF/UA1 identification
taggedContent.SetPdfUA1Identification();
//Create font and brush
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 14), true);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
//Add a "document" element
PdfStructureElement document = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document);
//Add a "heading" element
PdfStructureElement heading1 = document.AppendChildElement(PdfStandardStructTypes.HeadingLevel1);
heading1.BeginMarkedContent(page);
string headingText = "What Is a Tagged PDF?";
page.Canvas.DrawString(headingText, font, brush, new PointF(0, 0));
heading1.EndMarkedContent(page);
//Add a "paragraph" element
PdfStructureElement paragraph = document.AppendChildElement(PdfStandardStructTypes.Paragraph);
paragraph.BeginMarkedContent(page);
string paragraphText = "“Tagged PDF” doesn’t seem like a life-changing term. But for some, it is. For people who are " +
"blind or have low vision and use assistive technology (such as screen readers and connected Braille displays) to " +
"access information, an untagged PDF means they are missing out on information contained in the document because assistive " +
"technology cannot “read” untagged PDFs. Digital accessibility has opened up so many avenues to information that were once " +
"closed to people with visual disabilities, but PDFs often get left out of the equation.";
RectangleF rect = new RectangleF(0, 30, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height);
page.Canvas.DrawString(paragraphText, font, brush, rect);
paragraph.EndMarkedContent(page);
//Add a "figure" element to
PdfStructureElement figure = document.AppendChildElement(PdfStandardStructTypes.Figure);
figure.BeginMarkedContent(page);
PdfImage image = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\pdfua.png");
page.Canvas.DrawImage(image, new PointF(0, 150));
figure.EndMarkedContent(page);
//Add a "table" element
PdfStructureElement table = document.AppendChildElement(PdfStandardStructTypes.Table);
table.BeginMarkedContent(page);
PdfTable pdfTable = new PdfTable();
pdfTable.Style.DefaultStyle.Font = font;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Age");
dataTable.Columns.Add("Sex");
dataTable.Rows.Add(new string[] { "John", "22", "Male" });
dataTable.Rows.Add(new string[] { "Katty", "25", "Female" });
pdfTable.DataSource = dataTable;
pdfTable.Style.ShowHeader = true;
pdfTable.Draw(page.Canvas, new PointF(0, 280), 300f);
table.EndMarkedContent(page);
//Save the document to file
doc.SaveToFile("CreatePDFUA.pdf");
}
}
}
【VB.NET】
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interchange.TaggedPdf
Imports Spire.Pdf.Tables
Imports System.Data
Imports System.Drawing
Namespace CreatePDFUA
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Add a page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4,New PdfMargins(20))
'Set tab order
page.SetTabOrder(TabOrder.Structure)
'Create an object of PdfTaggedContent class
Dim taggedContent As PdfTaggedContent = New PdfTaggedContent(doc)
'Set language and title for the document
taggedContent.SetLanguage("en-US")
taggedContent.SetTitle("test")
'Set PDF/UA1 identification
taggedContent.SetPdfUA1Identification()
'Create font and brush
Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Times New Roman",14),True)
Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Black)
'Add a "document" element
Dim document As PdfStructureElement = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document)
'Add a "heading" element
Dim heading1 As PdfStructureElement = document.AppendChildElement(PdfStandardStructTypes.HeadingLevel1)
heading1.BeginMarkedContent(page)
Dim headingText As String = "What Is a Tagged PDF?"
page.Canvas.DrawString(headingText,font,brush,New PointF(0,0))
heading1.EndMarkedContent(page)
'Add a "paragraph" element
Dim paragraph As PdfStructureElement = document.AppendChildElement(PdfStandardStructTypes.Paragraph)
paragraph.BeginMarkedContent(page)
String paragraphText = "“Tagged PDF” doesn’t seem like a life-changing term. But for some, it is. For people who are " +
"blind or have low vision and use assistive technology (such as screen readers and connected Braille displays) to " +
"access information, an untagged PDF means they are missing out on information contained in the document because assistive " +
"technology cannot “read” untagged PDFs. Digital accessibility has opened up so many avenues to information that were once " +
"closed to people with visual disabilities, but PDFs often get left out of the equation."
Dim rect As RectangleF = New RectangleF(0,30,page.Canvas.ClientSize.Width,page.Canvas.ClientSize.Height)
page.Canvas.DrawString(paragraphText, font, brush, rect)
paragraph.EndMarkedContent(page)
'Add a "figure" element to
Dim figure As PdfStructureElement = document.AppendChildElement(PdfStandardStructTypes.Figure)
figure.BeginMarkedContent(page)
Dim image As PdfImage = PdfImage.FromFile("C:\Users\Administrator\Desktop\pdfua.png")
page.Canvas.DrawImage(image,New PointF(0,150))
figure.EndMarkedContent(page)
'Add a "table" element
Dim table As PdfStructureElement = document.AppendChildElement(PdfStandardStructTypes.Table)
table.BeginMarkedContent(page)
Dim pdfTable As PdfTable = New PdfTable()
pdfTable.Style.DefaultStyle.Font = font
Dim dataTable As DataTable = New DataTable()
dataTable.Columns.Add("Name")
dataTable.Columns.Add("Age")
dataTable.Columns.Add("Sex")
Dim String() As dataTable.Rows.Add(New
{
"John", "22", "Male"
}
)
Dim String() As dataTable.Rows.Add(New
{
"Katty", "25", "Female"
}
)
pdfTable.DataSource = dataTable
pdfTable.Style.ShowHeader = True
pdfTable.Draw(page.Canvas,New PointF(0,280),300f)
table.EndMarkedContent(page)
'Save the document to file
doc.SaveToFile("CreatePDFUA.pdf")
End Sub
End Class
End Namespace
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn