Ошибка «ожидаемого объекта DICT» при открытии PDF с использованием Adobe AcrobatJAVA

Программисты JAVA общаются здесь
Anonymous
Ошибка «ожидаемого объекта DICT» при открытии PDF с использованием Adobe Acrobat

Сообщение Anonymous »

Это продолжение этого вопроса здесь < /p>
Мне успешно удалось сгенерировать образец PDF, который доступен, и с тегом изображения. Я хотел бы сделать код более надежным, позволив пользователям вводить массив байтов существующего PDF и для того, чтобы код мог добавить в PDF -файл Alt Text Text, который загружается из массива байтов. правильно и дает «ожидаемый объект дикта» ошибку, когда я пытаюсь открыть ее на Adobe Acrobat < /p>
, учитывая, что я даже не могу открыть PDF, любые предложения о том, какие проблемы могут быть и как я мог бы отладить их? PDF документ с нуля с одним маленьким изображением. Я пытаюсь отредактировать свой код, чтобы получить PDF в виде байтового массива < /p>
static void main(String[] args) throws IOException {
// This function returns PDF Byte Array
byte[] pdfBytes = getPDFByteArray();
InputStream inputStream = new ByteArrayInputStream(pdfBytes);
PDDocument document = PDDocument.load(inputStream);
int pagesnum = document.getNumberOfPages();

for (int i = 0; i < pagesnum; i++) {
int mcidCounter = 0;
int structParentCounter = 0;
PDPage page = document.getPage(i);

PDImageXObject image = PDImageXObject.createFromFile("image.jpg", document);
image.setStructParent(structParentCounter);

page.setStructParents(structParentCounter);

PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);

PDDocumentCatalog catalog = document.getDocumentCatalog();
PDStructureTreeRoot structureTreeRoot = catalog.getStructureTreeRoot();
if (structureTreeRoot == null) {
structureTreeRoot = new PDStructureTreeRoot();
catalog.setStructureTreeRoot(structureTreeRoot);
}

catalog.setLanguage("en-US");

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

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

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

PDStructureElement paragraphElement = new PDStructureElement(StandardStructureTypes.Figure, documentElement);
paragraphElement.setAlternateDescription("An example image for accessibility on page " + (i + 1));
paragraphElement.setPage(page);
documentElement.appendKid(paragraphElement);

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

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

contentStream.beginMarkedContent(COSName.MCID, PDPropertyList.create(markedContentDictionary));
contentStream.drawImage(image, 100, 500, 200, 150); // X, Y, Width, Height
contentStream.endMarkedContent();

contentStream.close();

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

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

structureTreeRoot.setParentTreeNextKey(structParentCounter + 1);
}
ByteArrayOutputStream arr = new ByteArrayOutputStream();
document.save(arr);
document.close();
}


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

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