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

相关文章

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷