Skip to content

Commit 8a36e7d

Browse files
committed
Add roles and hierarchy support for text items
DEVSIX-8048
1 parent a74397b commit 8a36e7d

20 files changed

+1065
-66
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr;
24+
25+
/**
26+
* OCR properties passed to the OCR engine as part of {@link OcrProcessContext}.
27+
*/
28+
public interface IOcrProcessProperties {
29+
}

pdfocr-api/src/main/java/com/itextpdf/pdfocr/OcrPdfCreator.java

Lines changed: 250 additions & 58 deletions
Large diffs are not rendered by default.

pdfocr-api/src/main/java/com/itextpdf/pdfocr/OcrPdfCreatorProperties.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public class OcrPdfCreatorProperties {
9999

100100
private IMetaInfo metaInfo;
101101

102+
/**
103+
* Indicates whether the created pdf is tagged or not.
104+
*/
105+
private boolean tagged = false;
106+
102107
/**
103108
* Creates a new {@link OcrPdfCreatorProperties} instance.
104109
*/
@@ -369,6 +374,26 @@ public OcrPdfCreatorProperties setImageRotationHandler(
369374
return this;
370375
}
371376

377+
/**
378+
* Defines whether pdf document should be tagged or not.
379+
*
380+
* @param tagged {@code true} if the result pdf is expected to be tagged, {@code false} otherwise.
381+
* @return this {@link OcrPdfCreatorProperties} instance.
382+
*/
383+
public OcrPdfCreatorProperties setTagged(boolean tagged) {
384+
this.tagged = tagged;
385+
return this;
386+
}
387+
388+
/**
389+
* Retrieve information on whether pdf document should be tagged or not.
390+
*
391+
* @return {@code true} if the result pdf is expected to be tagged, {@code false} otherwise.
392+
*/
393+
public boolean isTagged() {
394+
return tagged;
395+
}
396+
372397
/**
373398
* Set meta info for this {@link OcrPdfCreatorProperties}.
374399
*

pdfocr-api/src/main/java/com/itextpdf/pdfocr/OcrProcessContext.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ This file is part of the iText (R) project.
2828
public class OcrProcessContext {
2929
private AbstractPdfOcrEventHelper ocrEventHelper;
3030

31+
private IOcrProcessProperties ocrProcessProperties;
32+
3133
/**
3234
* Creates an instance of ocr process context
3335
*
@@ -54,4 +56,22 @@ public AbstractPdfOcrEventHelper getOcrEventHelper() {
5456
public void setOcrEventHelper(AbstractPdfOcrEventHelper eventHelper) {
5557
this.ocrEventHelper = eventHelper;
5658
}
59+
60+
/**
61+
* Set extra OCR process properties.
62+
*
63+
* @param ocrProcessProperties extra OCR process properties.
64+
*/
65+
void setOcrProcessProperties(IOcrProcessProperties ocrProcessProperties) {
66+
this.ocrProcessProperties = ocrProcessProperties;
67+
}
68+
69+
/**
70+
* Get extra OCR process properties.
71+
*
72+
* @return extra OCR process properties.
73+
*/
74+
public IOcrProcessProperties getOcrProcessProperties() {
75+
return ocrProcessProperties;
76+
}
5777
}

pdfocr-api/src/main/java/com/itextpdf/pdfocr/TextInfo.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.pdfocr;
2424

2525
import com.itextpdf.kernel.geom.Rectangle;
26-
27-
import java.util.ArrayList;
28-
import java.util.Collections;
29-
import java.util.List;
30-
26+
import com.itextpdf.pdfocr.structuretree.LogicalStructureTreeItem;
3127

3228
/**
3329
* This class describes how recognized text is positioned on the image
@@ -45,6 +41,11 @@ public class TextInfo {
4541
*/
4642
private Rectangle bboxRect;
4743

44+
/**
45+
* If LogicalStructureTreeItem is set, then {@link TextInfo}s are expected to be in logical order.
46+
*/
47+
private LogicalStructureTreeItem logicalStructureTreeItem;
48+
4849
/**
4950
* Creates a new {@link TextInfo} instance.
5051
*/
@@ -107,4 +108,27 @@ public Rectangle getBboxRect() {
107108
public void setBboxRect(final Rectangle bbox) {
108109
this.bboxRect = new Rectangle(bbox);
109110
}
111+
112+
/**
113+
* Retrieves structure tree item for the text item.
114+
*
115+
* @return structure tree item.
116+
*/
117+
public LogicalStructureTreeItem getLogicalStructureTreeItem() {
118+
return logicalStructureTreeItem;
119+
}
120+
121+
/**
122+
* Sets logical structure tree parent item for the text info. It allows to organize text chunks
123+
* into logical hierarchy, e.g. specify document paragraphs, tables, etc.
124+
* <p>
125+
*
126+
* If LogicalStructureTreeItem is set, then the list of {@link TextInfo}s in {@link IOcrEngine#doImageOcr}
127+
* return value is expected to be in logical order.
128+
*
129+
* @param logicalStructureTreeItem structure tree item.
130+
*/
131+
public void setLogicalStructureTreeItem(LogicalStructureTreeItem logicalStructureTreeItem) {
132+
this.logicalStructureTreeItem = logicalStructureTreeItem;
133+
}
110134
}

pdfocr-api/src/main/java/com/itextpdf/pdfocr/exceptions/PdfOcrExceptionMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class PdfOcrExceptionMessageConstant {
3333
public static final String CANNOT_CREATE_PDF_DOCUMENT = "Cannot create PDF document: {0}";
3434
public static final String STATISTICS_EVENT_TYPE_CANT_BE_NULL = "Statistics event type can't be null";
3535
public static final String STATISTICS_EVENT_TYPE_IS_NOT_DETECTED = "Statistics event type is not detected.";
36+
public static final String TAGGING_IS_NOT_SUPPORTED = "Tagging is not supported by the OCR engine.";
3637

3738
private PdfOcrExceptionMessageConstant() {
3839
//Private constructor will prevent the instantiation of this class directly
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.itextpdf.pdfocr.structuretree;
2+
3+
/**
4+
* This class represents artifact structure tree item. Attaching such item to the text info means that
5+
* the text will be marked as artifact.
6+
*/
7+
public final class ArtifactItem extends LogicalStructureTreeItem {
8+
private final static ArtifactItem ARTIFACT_INSTANCE = new ArtifactItem();
9+
10+
private ArtifactItem() {
11+
super();
12+
}
13+
14+
/**
15+
* Retrieve an instance of {@link ArtifactItem}.
16+
*
17+
* @return an instance of {@link ArtifactItem}.
18+
*/
19+
public static ArtifactItem getInstance() {
20+
return ARTIFACT_INSTANCE;
21+
}
22+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr.structuretree;
24+
25+
import com.itextpdf.kernel.pdf.tagutils.AccessibilityProperties;
26+
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
/**
31+
* This class represents structure tree item of the text item put into the pdf document.
32+
* See {@link com.itextpdf.pdfocr.TextInfo}.
33+
*/
34+
public class LogicalStructureTreeItem {
35+
36+
private AccessibilityProperties accessibilityProperties;
37+
private List<LogicalStructureTreeItem> children = new ArrayList<>();
38+
private LogicalStructureTreeItem parent;
39+
40+
/**
41+
* Instantiate a new {@link LogicalStructureTreeItem} instance.
42+
*/
43+
public LogicalStructureTreeItem() {
44+
this(null);
45+
}
46+
47+
/**
48+
* Instantiate a new {@link LogicalStructureTreeItem} instance.
49+
*
50+
* @param accessibilityProperties properties to define and describe pdf structure elements.
51+
*/
52+
public LogicalStructureTreeItem(AccessibilityProperties accessibilityProperties) {
53+
this.accessibilityProperties = accessibilityProperties;
54+
}
55+
56+
/**
57+
* Retrieve structure tree element's properties.
58+
*
59+
* @return structure tree element's properties.
60+
*/
61+
public AccessibilityProperties getAccessibilityProperties() {
62+
return accessibilityProperties;
63+
}
64+
65+
/**
66+
* Set structure tree element's properties.
67+
*
68+
* @param accessibilityProperties structure tree element's properties.
69+
* @return this {@link LogicalStructureTreeItem} instance.
70+
*/
71+
public LogicalStructureTreeItem setAccessibilityProperties(AccessibilityProperties accessibilityProperties) {
72+
this.accessibilityProperties = accessibilityProperties;
73+
return this;
74+
}
75+
76+
/**
77+
* Retrieve parent structure tree item.
78+
*
79+
* @return parent structure tree item.
80+
*/
81+
public LogicalStructureTreeItem getParent() {
82+
return parent;
83+
}
84+
85+
/**
86+
* Add child structure tree item.
87+
*
88+
* @param child child structure tree item.
89+
* @return this {@link LogicalStructureTreeItem} instance.
90+
*/
91+
public LogicalStructureTreeItem addChild(LogicalStructureTreeItem child) {
92+
children.add(child);
93+
if (child.getParent() != null) {
94+
child.getParent().removeChild(child);
95+
}
96+
child.parent = this;
97+
98+
return this;
99+
}
100+
101+
/**
102+
* Remove child structure tree item.
103+
*
104+
* @param child child structure tree item.
105+
* @return {@code true} if the child was removed, {@code false} otherwise.
106+
*/
107+
public boolean removeChild(LogicalStructureTreeItem child) {
108+
if (children.remove(child)) {
109+
child.parent = null;
110+
return true;
111+
}
112+
113+
return false;
114+
}
115+
116+
/**
117+
* Retrieve all child structure tree items.
118+
*
119+
* @return all child structure tree items.
120+
*/
121+
public List<LogicalStructureTreeItem> getChildren() {
122+
return children;
123+
}
124+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr.structuretree;
24+
25+
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
26+
import com.itextpdf.kernel.pdf.tagutils.DefaultAccessibilityProperties;
27+
28+
/**
29+
* A convenience class to associate certain text items with the paragraph structure item.
30+
*/
31+
public class ParagraphTreeItem extends LogicalStructureTreeItem {
32+
33+
/**
34+
* Instantiate a new {@link ParagraphTreeItem} instance.
35+
*/
36+
public ParagraphTreeItem() {
37+
super(new DefaultAccessibilityProperties(StandardRoles.P));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr.structuretree;
24+
25+
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
26+
import com.itextpdf.kernel.pdf.tagutils.DefaultAccessibilityProperties;
27+
28+
/**
29+
* A convenience class to associate certain text items with the span structure item.
30+
*/
31+
public class SpanTreeItem extends LogicalStructureTreeItem {
32+
33+
/**
34+
* Instantiate a new {@link SpanTreeItem} instance.
35+
*/
36+
public SpanTreeItem() {
37+
super(new DefaultAccessibilityProperties(StandardRoles.SPAN));
38+
}
39+
}

0 commit comments

Comments
 (0)