标记的PDF是一个PDF文件,其中包含有关文档中元素内容的其他元数据。标签是一个无形的标签,可以告诉辅助技术用户页面上的内容。本文将分享如何使用java的spire.pdf编程创建标记的PDF文档。
导入依赖性(2种方法)
(Java的Spire.pdf是一个用于处理PDF文件的商业库。如果要从生成的文档中删除评估消息,则可以contact to get a 30-day trial license。)
方法1: Download the library并解开它。然后将spire.pdf.jar文件添加到您的项目中作为依赖项。
方法2:通过将以下配置添加到pom.xml。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>8.11.8</version>
</dependency>
</dependencies>
示例代码
要创建一个标记的PDF文件,您需要首先创建一个 pdftaggedContent 类的对象。然后,使用 pdftaggedContent.getStructureTreot()。appendChildelement()方法,将元素添加到根部。完整的示例代码如下所示。
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.interchange.taggedpdf.PdfStandardStructTypes;
import com.spire.pdf.interchange.taggedpdf.PdfStructureElement;
import com.spire.pdf.interchange.taggedpdf.PdfTaggedContent;
import com.spire.pdf.tables.PdfTable;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class CreateTaggedPdf {
public static void main(String[] args) throws Exception {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, new PdfMargins(20));
//Set tab order
page.setTabOrder(TabOrder.Structure);
//Create an object of PdfTaggedContent class
PdfTaggedContent taggedContent = new PdfTaggedContent(doc);
//Set language and title for the document
taggedContent.setLanguage("en-US");
taggedContent.setTitle("Create Tagged PDF in Java");
//Set PDF/UA1 identification
taggedContent.setPdfUA1Identification();
//Create font and brush
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,14), true);
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//Add a "document" element
PdfStructureElement document = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);
//Add a "heading" element
PdfStructureElement heading1 = document.appendChildElement(PdfStandardStructTypes.HeadingLevel1);
heading1.beginMarkedContent(page);
String headingText = "What Is a Tagged PDF?";
page.getCanvas().drawString(headingText, font, brush, new Point2D.Float(0, 0));
heading1.endMarkedContent(page);
//Add a "paragraph" element
PdfStructureElement paragraph = document.appendChildElement(PdfStandardStructTypes.Paragraph);
paragraph.beginMarkedContent(page);
String paragraphText = "Tagged PDFs can mean the difference between accessing information and " +
"being unable to do everyday digital tasks like checking your bank balance, " +
"viewing your statement of benefits from your healthcare provider, accessing college materials, and shopping online." ;
Rectangle2D.Float rect = new Rectangle2D.Float(0, 30, (float) page.getCanvas().getClientSize().getWidth(), (float) page.getCanvas().getClientSize().getHeight());
page.getCanvas().drawString(paragraphText, font, brush, rect);
paragraph.endMarkedContent(page);
//Add a "table" element
PdfStructureElement table = document.appendChildElement(PdfStandardStructTypes.Table);
table.beginMarkedContent(page);
PdfTable pdfTable = new PdfTable();
pdfTable.getStyle().getDefaultStyle().setFont(font);
String[] data = {"Name;Age;Sex",
"John;22;Male",
"Katty;25;Female"
};
String[][] dataSource = new String[data.length][];
for (int i = 0; i < data.length; i++) {
dataSource[i] = data[i].split("[;]", -1);
}
pdfTable.setDataSource(dataSource);
pdfTable.getStyle().setShowHeader(true);
pdfTable.draw(page.getCanvas(), new Point2D.Float(0, 120), 300f);
table.endMarkedContent(page);
//Add a "figure" element
PdfStructureElement figure = document.appendChildElement(PdfStandardStructTypes.Figure);
figure.beginMarkedContent(page);
PdfImage image = PdfImage.fromFile("pdf.jpg");
page.getCanvas().drawImage(image, new Point2D.Float(0, 200));
figure.endMarkedContent(page);
//Save the document to file
doc.saveToFile("CreatePDFUA.pdf");
}
}