0

我正在尝试从头开始构建 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();

4

2 回答 2

1

以下代码为文本添加下划线并生成通过 PAC3 检查器和 Acrobat Preflight PDF/UA 检查器的 PDF/UA 输出。

public void testUnderline() throws IOException {
    // Create PDF/UA with underline text
    String filename = "./results/Underline.pdf";
    WriterProperties properties = new WriterProperties();
    properties.addUAXmpMetadata().setPdfVersion(PdfVersion.PDF_1_7);
    PdfWriter writer = new PdfWriter(filename, properties);
    pdfDoc = new PdfDocument(writer);
    //Make document tagged
    pdfDoc.setTagged();
    pdfDoc.getCatalog().setLang(new PdfString("en-US"));
    pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
    PdfDocumentInfo info = pdfDoc.getDocumentInfo();
    info.setTitle("Hello Underline!");
    document = new Document(pdfDoc);

    // Must embed font for PDF/UA
    byte[] inputBytes = Files.readAllBytes(Paths.get("./resources/fonts/opensans-regular.ttf"));
    boolean embedded = true;
    boolean cached = false;
    PdfFont font = PdfFontFactory.createFont(inputBytes, PdfEncodings.CP1252, embedded, cached);
  
    Text text = new Text("This is an underlined Text object");
    text.setFont(font);
    text.setFontSize(16F);
    text.setUnderline();
    Paragraph para = new Paragraph();
    para.add(text);
    document.add(para);
    document.close();
    System.out.println(CREATED + filename);
}
于 2021-04-07T11:52:01.313 回答
0

我怀疑问题在于 PDF/UA 输出需要嵌入所有字体,您可能正在使用内置字体。要使用类似以下的内容来加载字体以进行嵌入。

byte[] inputBytes = Files.readAllBytes(Paths.get("./resources/fonts/opensans-regular.ttf"));
boolean embedded = true;
boolean cached = false;
PdfFont font = setFont(inputBytes, PdfEncodings.CP1252, embedded, cached);

您可能还需要创建一个 Text 对象,然后为 Text 对象设置字体、磅值、下划线属性,最后将 Text 对象添加到 Paragraph。例如,

Text text = new Text("HEADER");
text.setFont(font);
int pointSize = 10;
text.setFontSize(pointSize);
text.setUnderline();
于 2021-04-06T10:20:42.957 回答