首頁 > 軟體

C#/VB.NET實現建立PDF/UA檔案的範例程式碼

2022-08-24 14:01:50

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檔案內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com