C#/VB.NET實現創建PDF/UA文件的示例代碼
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/
到此這篇關於C#/VB.NET實現創建PDF/UA文件的示例代碼的文章就介紹到這瞭,更多相關C#創建PDF/UA文件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- C#實現創建標簽PDF文件的示例代碼
- Python docx庫刪除復制paragraph及行高設置圖片插入示例
- JavaScript canvas實現刮刮樂案例
- C#實現在窗體上的統計圖效果
- C#實現PDF簽名時添加時間戳的2種方法(附VB.NET代碼)