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调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Python创建Excel的4种方式小结

《Python创建Excel的4种方式小结》这篇文章主要为大家详细介绍了Python中创建Excel的4种常见方式,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以学习一下... 目录库的安装代码1——pandas代码2——openpyxl代码3——xlsxwriterwww.cppcns.c

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE