首页 > 编程语言 >C#/VB.NET 创建PDF/UA文件

C#/VB.NET 创建PDF/UA文件

时间:2022-08-24 10:27:05浏览次数:99  
标签:Dim VB C# doc New PDF UA Pages

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

 

参考资料:
  1. https://docs.fileformat.com/pdf/ua/

 

—END—

 

相关阅读推荐C# 创建标签PDF文件

 

标签:Dim,VB,C#,doc,New,PDF,UA,Pages
From: https://www.cnblogs.com/Yesi/p/16618895.html

相关文章

  • c++单例模式
    单例模式的意图:一个类只能有一个实例。 //非线程安全版本1classSingleton{2private:3Singleton();4Singleton(constSingleton&);5public:......
  • 基于Docker做MySQL主从搭建与Django的读写分离
    基于Docker做MySQL主从搭建主从的作用:写数据数据时使用主库,从库只用来读数据,这样做能够减少数据库压力,主从搭建可以一主一从,也可以是一主多从。mysql主从配置的流程如图......
  • 关于npm ERR! ERESOLVE could not resolve 问题解决
    1、问题描述从代码仓库拉取代码到本地,执行npminstall命令安装项目依赖,提示如下图错误  问题出现的原因由于npm版本问题,npm不同版本库之间命令不兼容。解决办法:执......
  • Fecify 跨境电商系统,有哪些优势?
    独立站是最近几年重新火爆起来的跨境电商经营方式。人都喜欢一切都掌握在自己手里的感觉,而独立站的好处就是自己说了算,足够自由。因为可以自己说了算,风险自己把控,自己做......
  • LeetCode 206. 反转链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListN......
  • PerfView专题 (第十篇):洞察 C# 终结队列引发的内存泄漏
    一:背景C#程序内存泄漏的诱发因素有很多,但从顶层原理上来说,就是该销毁的用户根对象没有被销毁,从而导致内存中意料之外的对象无限堆积,导致内存暴涨,最终崩溃,这其中的一个......
  • c# 获取 文件夹及子文件夹中所有文件
    publicstaticvoidGetFiles(stringpath,List<string>FileList){DirectoryInfodir=newDirectoryInfo(path);FileInfo[]......
  • reactive函数
    <template><div><h1>vue3</h1><span>{{info.name}}-{{info.age}}</span><button@click="infobtn">修改info</button></div></template><scrip......
  • centos7 搭建dnsmasq做为内网本地DNS解析服务器
    1、安装dnsmasqyuminstall-ydnsmasq2、修改配置#备份dnsmasq.confcp/etc/dnsmasq.conf/etc/dnsmasq.conf.bak#清空文件内容echo>/etc/dnsmasq.conf#写......
  • 常见的编码转换(CRC32爆破)
    题目文件流程1.拿到手是一个加密的压缩包有点懵逼2.网上找了一下说是需要crc爆破CRC爆破脚本链接找到每个文件的循环冗余校验码然后使用脚本爆破,发现几个有意......