本文介紹了Java-PDFbox:為帶標(biāo)簽的PDF中的線(xiàn)條和下劃線(xiàn)創(chuàng)建構(gòu)件標(biāo)簽的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在從標(biāo)記的pdf創(chuàng)建輔助功能PDF。它顯示";路徑對(duì)象未標(biāo)記&q;錯(cuò)誤。PDF有線(xiàn)條和帶下劃線(xiàn)的文本。因此,我正在嘗試為未添加標(biāo)簽的行項(xiàng)目添加";artiture";標(biāo)記。我可以從PDFGraphicsStreamEngine
獲得這些行。有人能幫我這個(gè)忙嗎?
PDF頁(yè)面 | PAC3錯(cuò)誤 |
---|---|
推薦答案
您可以使用PdfContentStreamEditor
中的PdfContentStreamEditor
類(lèi)根據(jù)需要編輯頁(yè)面內(nèi)容流,方法如下:
PDDocument document = ...;
for (PDPage page : document.getDocumentCatalog().getPages()) {
PdfContentStreamEditor markEditor = new PdfContentStreamEditor(document, page) {
int markedContentDepth = 0;
@Override
public void beginMarkedContentSequence(COSName tag, COSDictionary properties) {
if (inArtifact) {
System.err.println("Structural error in content stream: Path not properly closed by path painting instruction.");
}
markedContentDepth++;
super.beginMarkedContentSequence(tag, properties);
}
@Override
public void endMarkedContentSequence() {
markedContentDepth--;
super.endMarkedContentSequence();
}
boolean inArtifact = false;
@Override
protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException {
String operatorString = operator.getName();
boolean unmarked = markedContentDepth == 0;
boolean inArtifactBefore = inArtifact;
if (unmarked && (!inArtifactBefore) && PATH_CONSTRUCTION.contains(operatorString)) {
super.write(contentStreamWriter, Operator.getOperator("BMC"), Collections.singletonList(COSName.ARTIFACT));
inArtifact = true;
}
super.write(contentStreamWriter, operator, operands);
if (unmarked && inArtifactBefore && PATH_PAINTING.contains(operatorString)) {
super.write(contentStreamWriter, Operator.getOperator("EMC"), Collections.emptyList());
inArtifact = false;
}
}
final List<String> PATH_CONSTRUCTION = Arrays.asList("m", "l", "c", "v", "y", "h", "re");
final List<String> PATH_PAINTING = Arrays.asList("s", "S", "f", "F", "f*", "B", "B*", "b", "b*", "n");
};
markEditor.processPage(page);
}
document.save(...);
(EditMarkedContent測(cè)試testMarkUnmarkedPathsAsArtifactsTradeSimple1
)
beginMarkedContentSequence
和endMarkedContentSequence
覆蓋跟蹤當(dāng)前標(biāo)記的內(nèi)容嵌套深度,特別是是否標(biāo)記了當(dāng)前內(nèi)容。
對(duì)于尚未標(biāo)記的指令,write
覆蓋會(huì)將未標(biāo)記的路徑構(gòu)建和繪制指令序列包含在/Artifact BMC ... EMC
中。
請(qǐng)注意,此代碼僅考慮頁(yè)面內(nèi)容流中的內(nèi)容,它不會(huì)下降為表單XObject、模式等。
此外,如果內(nèi)容流有錯(cuò)誤(例如,在沒(méi)有繪制的情況下構(gòu)建路徑),此代碼可能會(huì)添加額外的錯(cuò)誤(例如,不平衡的標(biāo)記內(nèi)容開(kāi)始和結(jié)束)。
這篇關(guān)于Java-PDFbox:為帶標(biāo)簽的PDF中的線(xiàn)條和下劃線(xiàn)創(chuàng)建構(gòu)件標(biāo)簽的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,