微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 iText 7 访问下划线文本

如何解决如何使用 iText 7 访问下划线文本

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

解决方法

以下代码为文本加下划线并生成通过 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);
}
,

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

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

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

Text text = new Text("HEADER");
text.setFont(font);
int pointSize = 10;
text.setFontSize(pointSize);
text.setUnderline();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。