Spire.PDF for .NET【文档操作】演示:创建标记的 PDF 文档

2024-08-27 04:52

本文主要是介绍Spire.PDF for .NET【文档操作】演示:创建标记的 PDF 文档,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

带标签的 PDF(也称为 PDF/UA)是一种包含底层标签树(类似于 HTML)的 PDF,用于定义文档的结构。这些标签可以帮助屏幕阅读器浏览整个文档而不会丢失任何信息。本文介绍如何使用Spire.PDF for .NET在 C# 和 VB.NET 中从头开始创建带标签的 PDF 。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载   

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。

PM> Install-Package Spire.PDF
创建具有丰富元素的标签 PDF

要在带标签的 PDF 文档中添加结构元素,我们必须首先创建PdfTaggedContent类的对象。然后,使用PdfTaggedContent.StructureTreeRoot.AppendChildElement()方法将元素添加到根。以下是使用 Spire.PDF for .NET 向带标签的 PDF 添加“标题”元素的详细步骤。

  • 创建一个PdfDocument对象并使用PdfDocument.Pages.Add()方法向其中添加一个页面。
  • 创建PdfTaggedContent类的对象。
  • 使用PdfTaggedContent.SetPdfUA1Identification()方法使文档符合 PDF/UA 识别。
  • 使用PdfTaggedContent.StructureTreeRoot.AppendChildElement()方法将“文档”元素添加到文档的根目录。
  • 使用PdfStructureElement.AppendChildElement()方法在“document”元素下添加“heading”元素。
  • 使用PdfStructureElement.BeginMarkedContent()方法添加开始标签,指示标题元素的开始。
  • 使用PdfPageBase.Canvas.DrawString()方法在页面上绘制标题文本。
  • 使用PdfStructureElement.BeginMarkedContent()方法添加结束标签,这意味着标题元素在此结束。
  • 使用PdfDocument.SaveToFile()方法将文档保存为 PDF 文件。

以下代码片段提供了一个示例,说明如何在 C# 和 VB.NET 中在标记的 PDF 文档中创建各种元素,包括文档、标题、段落、图形和表格。

【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.DrawingNamespace 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

C#/VB.NET: Create a Tagged PDF Document

这篇关于Spire.PDF for .NET【文档操作】演示:创建标记的 PDF 文档的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110687

相关文章

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

Python Pillow 库详解文档(最新推荐)

《PythonPillow库详解文档(最新推荐)》Pillow是Python中最流行的图像处理库,它是PythonImagingLibrary(PIL)的现代分支和继承者,本文给大家介绍Pytho... 目录python Pillow 库详解文档简介安装核心模块架构Image 模块 - 核心图像处理基本导入

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指