我正在尝试从头开始构建 PDF,并且它必须是可访问的(PDF/UA)。但是,当我尝试添加下划线文本时遇到了问题。可访问性检查器抱怨“路径不能有跨度作为父级”。当我检查生成的实际 PDF 时,我注意到路径没有被标记为工件。我的问题是,如何标记此路径?或者,如何正确添加下划线文本?代码片段是打击:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf",
(new WriterProperties()).AddUAXmpMetadata().SetPdfVersion(PdfVersion.PDF_1_7)));
Document document = new Document(pdfDoc, PageSize.A4);
//TAGGED PDF
pdfDoc.SetTagged();
pdfDoc.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
PdfDocumentInfo info = pdfDoc.GetDocumentInfo();
info.SetTitle("Decision No. 1234/12");
Paragraph header = new Paragraph("HEADER");
header.SetFont(fontDefault)
.SetBold()
.SetUnderline();//Set underline. A Path object was added by iText.
header.GetAccessibilityProperties().SetRole(StandardRoles.H1);
document.Add(header);
document.Close();
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo("output.pdf") { UseShellExecute = true };
process.Start();
编辑:似乎使下划线可访问的唯一方法是使用低级函数。我在下面发布我的代码:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf",
(new WriterProperties()).AddUAXmpMetadata().SetPdfVersion(PdfVersion.PDF_1_7)));
Document document = new Document(pdfDoc, PageSize.A4);
PdfFont font = PdfFontFactory.CreateFont("arial.ttf", true);
//TAGGED PDF
pdfDoc.SetTagged();
pdfDoc.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
PdfDocumentInfo info = pdfDoc.GetDocumentInfo();
info.SetTitle("Decision No. 1234/12");
//Method 1 - to create a underlined header
//The Path added for the underline is not accessible (Not tagged as Artifact).
Paragraph header = new Paragraph("HEADER");
header.SetFont(font)
.SetBold()
.SetUnderline(); //Path created but not tagged as Artifact.
header.GetAccessibilityProperties().SetRole(StandardRoles.H1);
document.Add(header);
//Method 2 - to create a underlined header
//The Path added and properly tagged as Artifact
PdfCanvas canvas = new PdfCanvas(pdfDoc.GetLastPage());
TagTreePointer tagPointer = new TagTreePointer(pdfDoc);
tagPointer.SetPageForTagging(pdfDoc.GetFirstPage());
tagPointer.AddTag(StandardRoles.H1).AddTag(StandardRoles.SPAN);
canvas
.BeginText()
.MoveText(50, 700)
.SetFontAndSize(font, 12)
.OpenTag(tagPointer.GetTagReference())
.ShowText("HEADER")
.CloseTag()
.EndText();
//Manually draw the underline (Path)
float w = font.GetWidth("HEADER", 12);
canvas
.MoveTo(50, 700 - 1)
.LineTo(50 + w, 700 - 1)
.SetLineWidth(0.5F)
.Stroke();
//Close document
document.Close();
//Open the PDF
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo("output.pdf") { UseShellExecute = true };
process.Start();