itext7 pdfhtml headers 和屏幕阅读器有问题吗?
Problems with itext7 pdfhtml headers and screen readers?
我正在使用 html 的 pdf 功能开发 iText7。
我刚刚注意到每个页面上设置的 Header 都有这个问题。 Header 未被屏幕读取 reader。
文档的其余部分与屏幕 reader 完美配合。我需要它来检查可访问性。
我是这样设置的 Header:
<html>
<head>
<link rel="stylesheet" href="{...}pdf/css/style.css" type="text/css" />
<style>
#header {
position: running(header);
}
@page {
margin-top: 100px;
@top-left {
content: element(header);
}
}
</style>
</head>
<body>
<div id="header">
<table width="100%">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><span class="...">{...}</span></td>
<td rowspan="2" class="... ..."><span><img src="{...}pdf/images/..." alt="..." /></span></td>
</tr>
<tr>
<td><span class="...">{...}</span></td>
</tr>
</table>
</div>...
...
...
对于屏幕 reader 我没有找到任何方法让它在纯 HTML 中工作。
如果将来有人有一些想法,一定会很感激。
目前我正在编写启动新页面的事件处理程序。
我正在做一些改进,但像这样:
public class SomePdfCreationClass {
public SomeReturnType createPdf {
...
Header headerHandler = new Header(String header1, List<String> subHeaders);
pdf.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
HtmlConverter.convertToPdf(htmlFile, pdf, converterProperties);
writer.close();
pdf.close();
...
}
protected class Header implements IEventHandler {
private String header1;
private List<String> subHeaders;
public Header(String header1, List<String> subHeaders) {
this.header1 = header1;
this.subHeaders = subHeaders;
}
@Override
public void handleEvent(Event event) {
try {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
PdfDictionary pdfObject = page.getPdfObject();
PdfCanvas headerPdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdf);
Rectangle headerRect = new Rectangle(35, 740, 495, 96);
Canvas headerCanvas = new Canvas(headerPdfCanvas, pdf, headerRect);
headerCanvas.setFontProvider(fontProviderService.getFontProvider());
PdfFont font = PdfFontFactory.createFont(staticResourcesPath + "...ttf",
PdfEncodings.IDENTITY_H, true);
headerCanvas.setFontSize(18).setFont(font);
headerCanvas.enableAutoTagging(page);
CreateHeaderContent(headerCanvas);
headerCanvas.close();
} catch (IOException e) {
...
}
}
private void CreateHeaderContent(Canvas canvas) throws MalformedURLException
{
Table table = new Table(UnitValue.createPercentArray(new float[] { 80, 20 } ));
table.setWidth(UnitValue.createPercentValue(100));
Cell cell1 = new Cell().add(new Paragraph(header1).setBold().setFontSize(11).setTextAlignment(TextAlignment.LEFT));
for (int i = 0; i < subHeaders.size(); i++) {
cell1.add(new Paragraph(subHeaders.get(i)).setFontSize(8).setTextAlignment(TextAlignment.LEFT));
}
cell1.setBorder(Border.NO_BORDER);
table.addCell(cell1);
ImageData imageData = ImageDataFactory.create(staticResourcesPath + "...png");
Image image = new Image(imageData);
image.getAccessibilityProperties().setAlternateDescription("...");
Cell cell2 = new Cell().add(image.setTextAlignment(TextAlignment.RIGHT));
cell2.setBorder(Border.NO_BORDER);
cell2.setVerticalAlignment(VerticalAlignment.MIDDLE);
table.addCell(cell2);
canvas.add(table);
}
}
}
这样做我不得不为 header 删除 html:
<html>
<head>
<link rel="stylesheet" href="..." type="text/css" />
</head>
<body>
...
我正在使用 html 的 pdf 功能开发 iText7。
我刚刚注意到每个页面上设置的 Header 都有这个问题。 Header 未被屏幕读取 reader。 文档的其余部分与屏幕 reader 完美配合。我需要它来检查可访问性。
我是这样设置的 Header:
<html>
<head>
<link rel="stylesheet" href="{...}pdf/css/style.css" type="text/css" />
<style>
#header {
position: running(header);
}
@page {
margin-top: 100px;
@top-left {
content: element(header);
}
}
</style>
</head>
<body>
<div id="header">
<table width="100%">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><span class="...">{...}</span></td>
<td rowspan="2" class="... ..."><span><img src="{...}pdf/images/..." alt="..." /></span></td>
</tr>
<tr>
<td><span class="...">{...}</span></td>
</tr>
</table>
</div>...
...
...
对于屏幕 reader 我没有找到任何方法让它在纯 HTML 中工作。
如果将来有人有一些想法,一定会很感激。
目前我正在编写启动新页面的事件处理程序。
我正在做一些改进,但像这样:
public class SomePdfCreationClass {
public SomeReturnType createPdf {
...
Header headerHandler = new Header(String header1, List<String> subHeaders);
pdf.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
HtmlConverter.convertToPdf(htmlFile, pdf, converterProperties);
writer.close();
pdf.close();
...
}
protected class Header implements IEventHandler {
private String header1;
private List<String> subHeaders;
public Header(String header1, List<String> subHeaders) {
this.header1 = header1;
this.subHeaders = subHeaders;
}
@Override
public void handleEvent(Event event) {
try {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
PdfDictionary pdfObject = page.getPdfObject();
PdfCanvas headerPdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdf);
Rectangle headerRect = new Rectangle(35, 740, 495, 96);
Canvas headerCanvas = new Canvas(headerPdfCanvas, pdf, headerRect);
headerCanvas.setFontProvider(fontProviderService.getFontProvider());
PdfFont font = PdfFontFactory.createFont(staticResourcesPath + "...ttf",
PdfEncodings.IDENTITY_H, true);
headerCanvas.setFontSize(18).setFont(font);
headerCanvas.enableAutoTagging(page);
CreateHeaderContent(headerCanvas);
headerCanvas.close();
} catch (IOException e) {
...
}
}
private void CreateHeaderContent(Canvas canvas) throws MalformedURLException
{
Table table = new Table(UnitValue.createPercentArray(new float[] { 80, 20 } ));
table.setWidth(UnitValue.createPercentValue(100));
Cell cell1 = new Cell().add(new Paragraph(header1).setBold().setFontSize(11).setTextAlignment(TextAlignment.LEFT));
for (int i = 0; i < subHeaders.size(); i++) {
cell1.add(new Paragraph(subHeaders.get(i)).setFontSize(8).setTextAlignment(TextAlignment.LEFT));
}
cell1.setBorder(Border.NO_BORDER);
table.addCell(cell1);
ImageData imageData = ImageDataFactory.create(staticResourcesPath + "...png");
Image image = new Image(imageData);
image.getAccessibilityProperties().setAlternateDescription("...");
Cell cell2 = new Cell().add(image.setTextAlignment(TextAlignment.RIGHT));
cell2.setBorder(Border.NO_BORDER);
cell2.setVerticalAlignment(VerticalAlignment.MIDDLE);
table.addCell(cell2);
canvas.add(table);
}
}
}
这样做我不得不为 header 删除 html:
<html>
<head>
<link rel="stylesheet" href="..." type="text/css" />
</head>
<body>
...