PDFBox 3.0.2: не может быть, казалось, правильно пометить изображение для доступностиJAVA

Программисты JAVA общаются здесь
Anonymous
PDFBox 3.0.2: не может быть, казалось, правильно пометить изображение для доступности

Сообщение Anonymous »

Я пытаюсь создать PDF -документ с лучшей доступностью. Это не нужно проходить каких -либо стандартов, но я хотел бы добавить Alt Text к изображениям. Я могу правильно пометить текст, но я не могу правильно пометить изображения при использовании аналогичного рабочего процесса. Я ссылался на этот пост и связанный с ним пост, чтобы создать мой текущий код. < /P>
Мой код до сих пор: < /p>

Код: Выделить всё

public static void main(String[] args) throws IOException {
int mcidCounter = 0;
int structParentCounter = 0;
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);

page.setStructParents(structParentCounter);

PDPageContentStream contentStream = null;
try {
contentStream = new PDPageContentStream(document, page);
} catch (IOException e) {
throw new RuntimeException(e);
}

PDImageXObject pdImage = PDImageXObject.createFromFile("image_file", document);

PDDocumentCatalog catalog = document.getDocumentCatalog();
PDStructureTreeRoot structureTreeRoot = new PDStructureTreeRoot();
catalog.setStructureTreeRoot(structureTreeRoot);

PDViewerPreferences prefs = new PDViewerPreferences(new COSDictionary());
prefs.setDisplayDocTitle(true);
catalog.setViewerPreferences(prefs);

PDMarkInfo markInfo = new PDMarkInfo();
markInfo.setMarked(true);
catalog.setMarkInfo(markInfo);

PDStructureElement documentElement = new PDStructureElement(StandardStructureTypes.DOCUMENT, structureTreeRoot);
structureTreeRoot.appendKid(documentElement);

PDStructureElement paragraphElement = new PDStructureElement(StandardStructureTypes.P, documentElement);
paragraphElement.setPage(page);
documentElement.appendKid(paragraphElement);

COSDictionary markedContentDictionary = new COSDictionary();
markedContentDictionary.setInt(COSName.MCID, mcidCounter);

PDMarkedContentReference mcr = new PDMarkedContentReference();
mcr.setMCID(mcidCounter);
paragraphElement.appendKid(mcr);

contentStream.beginMarkedContent(COSName.P, PDPropertyList.create(markedContentDictionary));
contentStream.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD), 12);
contentStream.beginText();
contentStream.newLineAtOffset(50, 700);
contentStream.showText("Document Title");
contentStream.endText();
contentStream.endMarkedContent();

PDStructureElement figureElement = new PDStructureElement(StandardStructureTypes.Figure, documentElement);
figureElement.setPage(page);
figureElement.setAlternateDescription("Alternate Image Description");
documentElement.appendKid(figureElement);

COSDictionary markedContentDictionary3 = new COSDictionary();
markedContentDictionary3.setInt(COSName.MCID, mcidCounter + 2);
markedContentDictionary3.setString(COSName.ALT, "Alternate Image Description");

PDMarkedContentReference mcr3 = new PDMarkedContentReference();
mcr3.setMCID(mcidCounter + 2);
figureElement.appendKid(mcr3);

contentStream.beginMarkedContent(COSName.IMAGE, PDPropertyList.create(markedContentDictionary3));
contentStream.drawImage(pdImage, 50, 0);
contentStream.endMarkedContent();

contentStream.close();

COSDictionary parentTreeRoot = new COSDictionary();
PDNumberTreeNode parentTree = new PDNumberTreeNode(parentTreeRoot, COSBase.class);

Map parentTreeMap = new HashMap();
parentTreeMap.put(structParentCounter, paragraphElement);
parentTree.setNumbers(parentTreeMap);
structureTreeRoot.setParentTree(parentTree);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream);

byte[] pdfBytes = outputStream.toByteArray();
document.close();

Path actualPath = Path.of("test.pdf");
Files.write(actualPath, pdfBytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
Это внутренняя структура PDF
.

Я не вижу никакого дерева структуры, даже если я связываю его с каталогом документов. Я должен видеть?

Подробнее здесь: https://stackoverflow.com/questions/794 ... essibility

Вернуться в «JAVA»