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

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

时间:2022-12-30 12:01:15浏览次数:62  
标签: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

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

 

参考资料:

  1. ​PDF/UA File Format​

—END—

标签:Dim,VB,C#,doc,New,PDF,UA,Pages
From: https://blog.51cto.com/u_15656056/5979855

相关文章

  • Spire.Doc API 可以在下载啦!
    近期,我们会将E-iceblue公司发布的系列产品帮助文档API陆续提交到.今天我们提交的是Spire.DocAPI.先来看看Spire.Doc产品的API样式图:Spire.DocAPI详细罗列了Spire.Doc......
  • C# PPT 为形状设置三维效果
    在PPT中,形状是非常重要的元素。3-D形状,立体多元,给人耳目一新的感觉。在幻灯片中添加3-D效果形状,必然会为PPT的整体效果增色不少。为形状设置三维格式时,可设置棱台,轮廓线,表面......
  • Leetcode209
    209.MinimumSizeSubarraySum        i , s , l = 0, 0, 0        for j in range(len(nums)):            s += nums[j......
  • C# 获取电脑SN号
    C#获取电脑SN号 ///<summary>///获得电脑SN号///</summary>publicstaticstringGetPcsnString(){varpcs......
  • echartsX轴时间轴固定显示0-24小时
    时间格式化使用moment.js,按需求添加,如不需要请手动格式化至相应格式npminstallmoment--save获取当天起始时间moment().startOf('day').format('YYYY-MM-DDHH......
  • Android笔记--Activity--启停活动页面
    Activity启动从当前页面跳转到新的页面:startActivity(newIntent(原页面.this,目标页面.class))而若是从当前页面返回到上一个页面,相当于关闭当前页面,使用finish()代码返......
  • HWC和CHW数据格式
     原文链接↓NCHWbatch批量大小channels,特征图通道数特征图的高特征图的宽 NCHW中,“N”batch批量大小,“C”channels特征图通道数,“H”特征图的高,和“W”特......
  • SpringCloud Gateway源码中的适配器模式
    SpringCloudgateway中有两种过滤器,路由过滤器(接口为GatewayFilter)和全局过滤器(接口为GlobalFilter);路由过滤器GatewayFilter的定义:1publicinterfaceGatewayFilter......
  • 在windows上构建OpenCascade
    基于作者QuaoarsWorkshop的视频OpenCascadeLessons,讲的非常详细,观看需要魔法什么是OCCT?.首先,OpenCASCADETechnologySDK是一个几何建模库,或者更简单地说,它是一个几......
  • CA如何吊销签署过的证书
    CA如何吊销签署过的证书1:客户端获取要吊销证书的serial(在使用证书上的主机执行)opensslx509-inhttpd.crt-noout-serial-subject2:拿到证书的编号后,通过编号吊销证......