C#/VB.NET实现创建PDF/UA文件的示例代码
作者:E-iceblue 时间:2023-05-02 23:31:51
1.什么是PDF/UA文件
PDF/UA,即Universally Accessible PDF,该格式的PDF文件是于2012年8月以ISO标准14289-1发布的、具有普遍可访问的PDF文档标准。为带标签的PDF文档(Tagged PDF Document)和表单的开发人员和作者设定了明确的规则。(PDF/UA-1是作为PDF/UA标准的第一部分,PDF/UA-2作为PDF/UA标准的第二部分)[1]
2.文件格式要求
PDF/UA在其基础上有着明确的要求,即该标准都是基于PDF标签,所有PDF/UA文档都必须以PDF标签来正确标记,并且,要求标签在语义上适当,并按逻辑顺序排列。[1]
3.如何创建PDF/UA文件
PDF/UA文件作为PDF文件的一种标准格式,要创建该标准的的PDF文档,可使用调用PDF API的方法来创建。下面,是以Spire.PDF for .NET来创建的方法。
注:可在Visual Studio 中通过NuGet搜索“Spire.PDF”安装dll到程序中;
或者下载包(hotfix版本)到本地,手动添加本地路径下的Spire.Pdf.dll文件到程序。
C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interchange.TaggedPdf;
using System.Drawing;
namespace CreatePDFUAfile
{
class Program
{
static void Main(string[] args)
{
//创建PdfDocument类的对象
PdfDocument doc = new PdfDocument();
//添加一页
doc.Pages.Add();
//设置tab order
doc.Pages[0].SetTabOrder(TabOrder.Structure);
//创建PdfTaggedContent类的对象
PdfTaggedContent taggedContent = new PdfTaggedContent(doc);
taggedContent.SetLanguage("en-US");
taggedContent.SetTitle("test");
//设置PDF/UA1验证
taggedContent.SetPdfUA1Identification();
//创建字体、画刷
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 10), true);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
//添加要素
PdfStructureElement article = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document);
PdfStructureElement paragraph1 = article.AppendChildElement(PdfStandardStructTypes.Paragraph);
PdfStructureElement span1 = paragraph1.AppendChildElement(PdfStandardStructTypes.Span);
span1.BeginMarkedContent(doc.Pages[0]);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify);
doc.Pages[0].Canvas.DrawString("PDF/UA was published as ISO Standard 14289-1 in August 2012" +
" and is by far the first complete definition " +
"of a set of requirements for universally accessible PDF documents.",
font, brush, new Rectangle(40, 80, 450, 80), format);
span1.EndMarkedContent(doc.Pages[0]);
PdfStructureElement paragraph2 = article.AppendChildElement(PdfStandardStructTypes.Paragraph);
paragraph2.BeginMarkedContent(doc.Pages[0]);
doc.Pages[0].Canvas.DrawString("The PDF/UA has some definite requirements at its base that act as the essence of this standard.",
font, brush, new Rectangle(40, 120, 450, 60), format);
paragraph2.EndMarkedContent(doc.Pages[0]);
PdfStructureElement figure1 = article.AppendChildElement(PdfStandardStructTypes.Figure);
//Set Alternate text
figure1.Alt = "replacement text1";
figure1.BeginMarkedContent(doc.Pages[0], null);
PdfImage image = PdfImage.FromFile(@"logo.jpg");
doc.Pages[0].Canvas.DrawImage(image, new PointF(40, 200), new SizeF(100, 100));
figure1.EndMarkedContent(doc.Pages[0]);
PdfStructureElement figure2 = article.AppendChildElement(PdfStandardStructTypes.Figure);
//Set Alternate text
figure2.Alt = "replacement text2";
figure2.BeginMarkedContent(doc.Pages[0], null);
doc.Pages[0].Canvas.DrawRectangle(PdfPens.Black, new Rectangle(300, 200, 100, 100));
figure2.EndMarkedContent(doc.Pages[0]);
//保存文档
doc.SaveToFile("CreatePDFUAFile_result.pdf");
System.Diagnostics.Process.Start("CreatePDFUAFile_result.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interchange.TaggedPdf
Imports System.Drawing
Namespace CreatePDFUAfile
Class Program
Private Shared Sub Main(args As String())
'创建PdfDocument类的对象
Dim doc As New PdfDocument()
'添加一页
doc.Pages.Add()
'设置tab order
doc.Pages(0).SetTabOrder(TabOrder.[Structure])
'创建PdfTaggedContent类的对象
Dim taggedContent As New PdfTaggedContent(doc)
taggedContent.SetLanguage("en-US")
taggedContent.SetTitle("test")
'设置PDF/UA1验证
taggedContent.SetPdfUA1Identification()
'创建字体、画刷
Dim font As New PdfTrueTypeFont(New Font("Times New Roman", 10), True)
Dim brush As New PdfSolidBrush(Color.Black)
'添加要素
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(doc.Pages(0))
Dim format As New PdfStringFormat(PdfTextAlignment.Justify)
doc.Pages(0).Canvas.DrawString("PDF/UA was published as ISO Standard 14289-1 in August 2012" + " and is by far the first complete definition " + "of a set of requirements for universally accessible PDF documents.", font, brush, New Rectangle(40, 80, 450, 80), format)
span1.EndMarkedContent(doc.Pages(0))
Dim paragraph2 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Paragraph)
paragraph2.BeginMarkedContent(doc.Pages(0))
doc.Pages(0).Canvas.DrawString("The PDF/UA has some definite requirements at its base that act as the essence of this standard.", font, brush, New Rectangle(40, 120, 450, 60), format)
paragraph2.EndMarkedContent(doc.Pages(0))
Dim figure1 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Figure)
'Set Alternate text
figure1.Alt = "replacement text1"
figure1.BeginMarkedContent(doc.Pages(0), Nothing)
Dim image As PdfImage = PdfImage.FromFile("logo.jpg")
doc.Pages(0).Canvas.DrawImage(image, New PointF(40, 200), New SizeF(100, 100))
figure1.EndMarkedContent(doc.Pages(0))
Dim figure2 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Figure)
'Set Alternate text
figure2.Alt = "replacement text2"
figure2.BeginMarkedContent(doc.Pages(0), Nothing)
doc.Pages(0).Canvas.DrawRectangle(PdfPens.Black, New Rectangle(300, 200, 100, 100))
figure2.EndMarkedContent(doc.Pages(0))
'保存文档
doc.SaveToFile("CreatePDFUAFile_result.pdf")
System.Diagnostics.Process.Start("CreatePDFUAFile_result.pdf")
End Sub
End Class
End Namespace
参考资料:
https://docs.fileformat.com/pdf/ua/
来源:https://www.cnblogs.com/Yesi/p/16618895.html
标签:C#,创建,PDF/UA
0
投稿
猜你喜欢
详解java中jvm虚拟机栈的作用
2022-06-06 14:08:44
Android从网络中获得一张图片并显示在屏幕上的实例详解
2023-11-03 01:17:43
Android中Java根据文件头获取文件类型的方法
2023-05-08 18:27:48
Java Map所有的值转为String类型
2022-09-05 11:53:09
Android自定义View实现带数字的进度条实例代码
2021-05-27 01:31:49
C#中重载重写和覆盖的定义与区别
2022-04-23 09:25:34
C#操作windows系统进程的方法
2023-09-01 03:13:26
C#在复杂多线程环境下使用读写锁同步写入文件
2021-06-16 03:45:53
Android实现图片文字轮播特效
2021-07-25 18:44:11
Java 远程调用失败重试的操作方法
2021-08-26 21:42:58
WinForm实现最小化到系统托盘方法实例详解
2023-10-05 05:53:30
Java守护线程实例详解_动力节点Java学院整理
2023-03-29 08:14:30
RabbitMQ消息中间件示例详解
2021-12-10 23:09:58
Java并发的CAS原理与ABA问题的讲解
2023-11-25 12:17:21
springboot+mybatis报错找不到实体类的问题
2023-05-20 02:04:10
Java基础之多线程
2022-11-30 11:01:43
java中不定长参数的实例用法
2021-06-17 02:49:35
关于fastjson的@JSONField注解的一些问题(详解)
2021-11-23 09:09:34
通过特性(attribute)为枚举添加更多信息示例
2023-10-10 09:22:06
java调用外部程序的方法及代码演示
2023-11-13 22:42:55