C# 创建标签PDF文件 茶杯狐

   2023-02-09 学习力0
核心提示:Q1:关于“标签PDF文件(Tagged PDF)”标签PDF文件包含描述文档结构和各种文档元素顺序的元数据,是一种包含后端提供的可访问标记,管理阅读顺序和文档内容表示的逻辑结构的PDF文件[1]。Q2:关于“标签(Tag)”PDF标签是通过屏幕阅读器等支持技术访问PDF文

Q1:关于“标签PDF文件(Tagged PDF)”

标签PDF文件包含描述文档结构和各种文档元素顺序的元数据,是一种包含后端提供的可访问标记,管理阅读顺序和文档内容表示的逻辑结构的PDF文件[1]

Q2:关于“标签(Tag)”

PDF标签是通过屏幕阅读器等支持技术访问PDF文档内容的关键。PDF标记在层次结构或标记树(tag tree)中排列PDF内容[1]

这里的标签是一种不可见的标签,它提供关于PDF文档内容的重要信息。带标签的PDF包含许多不同类型的标签,但最常用的是文本、替代文本(图像的替代文本)、标题、链接和链接描述[2]

Q3:PDF标签的用处及意义

添加PDF标签不会改变文档的视觉外观,但它提供了一个不可见的层,用于格式化文档与屏幕阅读器协作工作,这就使得从PDF文件中提取文本和图形变得更容易,并帮助屏幕阅读器以正确的顺序显示文件内容。[2]

PDF标签还可以用于将内容传输到屏幕较小的设备,如智能手机和平板电脑。[2]

Q4:如何创建标签PDF文件

本文将要介绍的创建方法是以后端C#程序代码的方式来创建标签PDF文件。创建时,通过NuGet安装引用PDF API-Spire.PDF for .NET,调用其提供的类及相关方法来标记内容、结构元素等。

C#

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interchange.TaggedPdf;
using System.Drawing;

namespace CreateTaggedPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PdfDocument类的对象
            PdfDocument pdf = new PdfDocument();

            //添加一页
            pdf.Pages.Add(PdfPageSize.A4);

            //设置tab order
            pdf.Pages[0].SetTabOrder(TabOrder.Structure);

            //创建PdfTaggedContent类的对象
            PdfTaggedContent taggedContent = new PdfTaggedContent(pdf);
            taggedContent.SetLanguage("en-US");
            taggedContent.SetTitle("test");

            //创建字体、画刷、字符串格式
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 10), true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            //添加elements
            PdfStructureElement article = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document);
            PdfStructureElement paragraph1 = article.AppendChildElement(PdfStandardStructTypes.Paragraph);
            PdfStructureElement span1 = paragraph1.AppendChildElement(PdfStandardStructTypes.Span);
            span1.BeginMarkedContent(pdf.Pages[0]);
            //绘制内容到页面
            pdf.Pages[0].Canvas.DrawString("A PDF tag is the key to accessing the contents of PDF documents with supporting technologies such as screen readers. ", font, brush, new Rectangle(40, 0, 480, 80), format);
            span1.EndMarkedContent(pdf.Pages[0]);

            PdfStructureElement paragraph2 = article.AppendChildElement(PdfStandardStructTypes.Paragraph);
            paragraph2.BeginMarkedContent(pdf.Pages[0]);
            pdf.Pages[0].Canvas.DrawString("A PDF tag arranges the PDF content in a hierarchical architecture or tag tree.", font, brush, new Rectangle(40, 80, 480, 80), format);
            paragraph2.EndMarkedContent(pdf.Pages[0]);

            PdfStructureElement figure1 = article.AppendChildElement(PdfStandardStructTypes.Figure);
            //Set Alternate text 
            figure1.Alt = "replacement text1";
            figure1.BeginMarkedContent(pdf.Pages[0], null);
            PdfImage image = PdfImage.FromFile(@"logo.png");
            pdf.Pages[0].Canvas.DrawImage(image, new PointF(40, 200), new SizeF(100, 100));//绘制图片到页面
            figure1.EndMarkedContent(pdf.Pages[0]);

            PdfStructureElement figure2 = article.AppendChildElement(PdfStandardStructTypes.Figure);
            //Set Alternate text
            figure2.Alt = "replacement text2";
            figure2.BeginMarkedContent(pdf.Pages[0], null);
            pdf.Pages[0].Canvas.DrawRectangle(PdfPens.Black, new Rectangle(300, 200, 100, 100));
            figure2.EndMarkedContent(pdf.Pages[0]);

            //保存文档          
            pdf.SaveToFile("CreateTaggedFile_result.pdf");
        }
    }
}

vb.net

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interchange.TaggedPdf
Imports System.Drawing

Namespace CreateTaggedPDF
    Class Program
        Private Shared Sub Main(args As String())
            '创建PdfDocument类的对象
            Dim pdf As New PdfDocument()

            '添加一页
            pdf.Pages.Add(PdfPageSize.A4)

            '设置tab order
            pdf.Pages(0).SetTabOrder(TabOrder.[Structure])

            '创建PdfTaggedContent类的对象
            Dim taggedContent As New PdfTaggedContent(pdf)
            taggedContent.SetLanguage("en-US")
            taggedContent.SetTitle("test")

            '创建字体、画刷、字符串格式
            Dim font As New PdfTrueTypeFont(New Font("Times New Roman", 10), True)
            Dim brush As New PdfSolidBrush(Color.Black)
            Dim format As New PdfStringFormat(PdfTextAlignment.Left)

            '添加elements
            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(pdf.Pages(0))
            '绘制内容到页面
            pdf.Pages(0).Canvas.DrawString("A PDF tag is the key to accessing the contents of PDF documents with supporting technologies such as screen readers. ", font, brush, New Rectangle(40, 0, 480, 80), format)
            span1.EndMarkedContent(pdf.Pages(0))

            Dim paragraph2 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Paragraph)
            paragraph2.BeginMarkedContent(pdf.Pages(0))
            pdf.Pages(0).Canvas.DrawString("A PDF tag arranges the PDF content in a hierarchical architecture or tag tree.", font, brush, New Rectangle(40, 80, 480, 80), format)
            paragraph2.EndMarkedContent(pdf.Pages(0))

            Dim figure1 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Figure)
            'Set Alternate text 
            figure1.Alt = "replacement text1"
            figure1.BeginMarkedContent(pdf.Pages(0), Nothing)
            Dim image As PdfImage = PdfImage.FromFile("logo.png")
            pdf.Pages(0).Canvas.DrawImage(image, New PointF(40, 200), New SizeF(100, 100))
            '绘制图片到页面
            figure1.EndMarkedContent(pdf.Pages(0))

            Dim figure2 As PdfStructureElement = article.AppendChildElement(PdfStandardStructTypes.Figure)
            'Set Alternate text
            figure2.Alt = "replacement text2"
            figure2.BeginMarkedContent(pdf.Pages(0), Nothing)
            pdf.Pages(0).Canvas.DrawRectangle(PdfPens.Black, New Rectangle(300, 200, 100, 100))
            figure2.EndMarkedContent(pdf.Pages(0))

            '保存文档          
            pdf.SaveToFile("CreateTaggedFile_result.pdf")
            System.Diagnostics.Process.Start("CreateTaggedFile_result.pdf")
        End Sub
    End Class
End Namespace

参考资料:

[1]. https://247accessibledocuments.com/what-is-a-tagged-pdf/

[2]. https://accessibility-i.org/what-is-a-tagged-pdf/

 

—END—

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 使用C#编写一个.NET分析器(一) 使用csv模块的什么方法可以一次性将一行数据写入文件
    使用C#编写一个.NET分析器(一) 使用csv模块的
    译者注这是在Datadog公司任职的Kevin Gosse大佬使用C#编写.NET分析器的系列文章之一,在国内只有很少很少的人了解和研究.NET分析器,它常被用于APM(应用性能诊断)、IDE、诊断工具中,比如Datadog的APM,Visual Studio的分析器以及Rider和Reshaper等等。之前
    03-08
  • 跨语言调用C#代码的新方式-DllExport 跨语言调用本质
    跨语言调用C#代码的新方式-DllExport 跨语言调
    简介上一篇文章使用C#编写一个.NET分析器文章发布以后,很多小伙伴都对最新的NativeAOT函数导出比较感兴趣,今天故写一篇短文来介绍一下如何使用它。在以前,如果有其他语言需要调用C#编写的库,那基本上只有通过各种RPC的方式(HTTP、GRPC)或者引入一层C++
    03-08
  • 我比较了 Go 和 C# 的速度
    我比较了 Go 和 C# 的速度
    我在 Go 和 C# 之间进行了速度比较。我通常使用 C#,但我有机会使用 Go,并且由于传闻 Go 速度很快,所以我实际测量了它。测量内容我在 Go 和 C# 中执行了一个简单的循环和判断过程,以查看整数 2 到 N 是否为质数。来源是Github参考。测量模式 逻辑内核 8 Wi
    03-08
  • [C#]使用 AltCover 获得代码覆盖率 - E2E Test 和 Unit Test
    [C#]使用 AltCover 获得代码覆盖率 - E2E Test
    背景在 CI/CD 流程当中,测试是 CI 中很重要的部分。跟开发人员关系最大的就是单元测试,单元测试编写完成之后,我们可以使用 IDE 或者 dot cover 等工具获得单元测试对于业务代码的覆盖率。不过我们需要一个独立的 CLI 工具,这样我们才能够在 Jenkins 的 CI
  • C#中LINQ的Select与SelectMany函数如何使用 c反应蛋白高说明什么
    C#中LINQ的Select与SelectMany函数如何使用 c反
    本篇内容主要讲解“C#中LINQ的Select与SelectMany函数如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#中LINQ的Select与SelectMany函数如何使用”吧!LINQ的Select与SelectMany函数使用Select扩展函
    02-09 linqselect
  • PerfView专题 (第三篇):如何寻找 C# 中的 VirtualAlloc 内存泄漏
    PerfView专题 (第三篇):如何寻找 C# 中的 Virt
    一:背景上一篇我们聊到了如何用 PerfView 去侦察 NTHeap 的内存泄漏,这种内存泄漏往往是用 C 的 malloc 或者 C++ 的 new 分配而不释放所造成的,这一篇我们来聊一下由 VirtualAlloc 方法造成的泄漏如何去甄别?了解 VirtualAlloc 的朋友肯定说, C# 这种高
    02-09
  • Blazor和Vue对比学习(知识点杂锦3.04):Blazor中C#和JS互操作(超长文)
    Blazor和Vue对比学习(知识点杂锦3.04):Blazo
    C#和JS互操作的基本语法是比较简单的,但小知识点特别多,同时,受应用加载顺序、组件生命周期以及参数类型的影响,会有比较多坑,需要耐心的学习。在C#中调用JS的场景会比较多,特别是在WASM模式下,由于WebAssembly的限制,很多时候,还是需要借助JS去控制D
    02-09
  • 的键">C#怎么使用struct类型作为泛型Dictionary
    本文小编为大家详细介绍“C#怎么使用struct类型作为泛型DictionaryTKey,TValue的键”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#怎么使用struct类型作为泛型DictionaryTKey,TValue的键”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学
  • C#如何实现折半查找算法 彩票查询
    C#如何实现折半查找算法 彩票查询
    本篇内容主要讲解“C#如何实现折半查找算法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#如何实现折半查找算法”吧!折半查找,也叫二分查找,当在一个数组或集合中查找某个元素时,先定位出中间位置元素
    02-09
  • C#如何实现选择排序 c罗
    C#如何实现选择排序 c罗
    本篇内容主要讲解“C#如何实现选择排序”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#如何实现选择排序”吧!选择排序是一种低效的排序算法,大致过程是:遍历数组的每一个元素,先假设0号位置上的元素是最
    02-09
点击排行