Skip to content

Commit 8c272c2

Browse files
committed
Add Contents for link annotations
DEVSIX-8679
1 parent b68cd09 commit 8c272c2

File tree

16 files changed

+41
-31
lines changed

16 files changed

+41
-31
lines changed

src/main/java/com/itextpdf/html2pdf/attach/util/LinkHelper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url,
8585
linkAnnotation = (PdfLinkAnnotation) new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0)).setAction(PdfAction.createURI(url)).setFlags(PdfAnnotation.PRINT);
8686
}
8787
String alternateDescription = retrieveAlternativeDescription(element);
88-
if (container instanceof IAccessibleElement && alternateDescription != null) {
89-
((IAccessibleElement) container).getAccessibilityProperties().setAlternateDescription(alternateDescription);
88+
if (alternateDescription != null) {
89+
linkAnnotation.setContents(alternateDescription);
90+
if (container instanceof IAccessibleElement) {
91+
((IAccessibleElement) container).getAccessibilityProperties().setAlternateDescription(alternateDescription);
92+
}
9093
}
94+
9195
linkAnnotation.setBorder(new PdfArray(new float[]{0, 0, 0}));
9296
container.setProperty(Property.LINK_ANNOTATION, linkAnnotation);
9397
if (container instanceof ILeafElement && container instanceof IAccessibleElement) {

src/test/java/com/itextpdf/html2pdf/HtmlConverterPdfUA1UA2Test.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,56 +71,50 @@ public static void beforeClass() {
7171
}
7272

7373
@Test
74-
// TODO DEVSIX-8679 Apply links alternate description according to UA standard
7574
public void simpleLinkTest() throws IOException, InterruptedException, XMPException {
7675
String sourceHtml = SOURCE_FOLDER + "simpleLink.html";
7776
String cmpPdfUa1 = SOURCE_FOLDER + "cmp_simpleLinkUa1.pdf";
7877
String cmpPdfUa2 = SOURCE_FOLDER + "cmp_simpleLinkUa2.pdf";
79-
String destinationPdfUa1 = DESTINATION_FOLDER + "simpleLink.pdf";
80-
String destinationPdfUa2 = DESTINATION_FOLDER + "simpleLink.pdf";
78+
String destinationPdfUa1 = DESTINATION_FOLDER + "simpleLinkUa1.pdf";
79+
String destinationPdfUa2 = DESTINATION_FOLDER + "simpleLinkUa2.pdf";
8180

82-
String expectedUa1Message = MessageFormatUtil.format(
83-
PdfUAExceptionMessageConstants.ANNOTATION_OF_TYPE_0_SHOULD_HAVE_CONTENTS_OR_ALT_KEY,
84-
PdfName.Link.getValue());
85-
86-
convertToUa1AndCheckCompliance(sourceHtml, destinationPdfUa1, cmpPdfUa1, false, expectedUa1Message);
87-
// Expected valid UA-2 document because PDF/UA-2 does not require Contents in Link annotations
81+
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, true, null);
8882
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, true);
8983
}
9084

9185
@Test
92-
// TODO DEVSIX-8679 Apply links alternate description according to UA standard
9386
public void backwardLinkTest() throws IOException, InterruptedException, XMPException {
9487
String sourceHtml = SOURCE_FOLDER + "backwardLink.html";
9588
String cmpPdfUa1 = SOURCE_FOLDER + "cmp_backwardLinkUa1.pdf";
9689
String cmpPdfUa2 = SOURCE_FOLDER + "cmp_backwardLinkUa2.pdf";
9790
String destinationPdfUa1 = DESTINATION_FOLDER + "backwardLinkUa1.pdf";
9891
String destinationPdfUa2 = DESTINATION_FOLDER + "backwardLinkUa2.pdf";
9992

100-
String expectedUa1Message = MessageFormatUtil.format(
101-
PdfUAExceptionMessageConstants.ANNOTATION_OF_TYPE_0_SHOULD_HAVE_CONTENTS_OR_ALT_KEY,
102-
PdfName.Link.getValue());
103-
104-
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, false, expectedUa1Message);
105-
// Expected valid UA-2 document because PDF/UA-2 does not require Contents in Link annotations
93+
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, true, null);
10694
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, true);
10795
}
10896

10997
@Test
110-
// TODO DEVSIX-8679 Apply links alternate description according to UA standard
11198
public void imageLinkTest() throws IOException, InterruptedException, XMPException {
11299
String sourceHtml = SOURCE_FOLDER + "imageLink.html";
113100
String cmpPdfUa1 = SOURCE_FOLDER + "cmp_imageLinkUa1.pdf";
114101
String cmpPdfUa2 = SOURCE_FOLDER + "cmp_imageLinkUa2.pdf";
115102
String destinationPdfUa1 = DESTINATION_FOLDER + "imageLinkUa1.pdf";
116103
String destinationPdfUa2 = DESTINATION_FOLDER + "imageLinkUa2.pdf";
117104

118-
String expectedUa1Message = MessageFormatUtil.format(
119-
PdfUAExceptionMessageConstants.ANNOTATION_OF_TYPE_0_SHOULD_HAVE_CONTENTS_OR_ALT_KEY,
120-
PdfName.Link.getValue());
105+
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, true, null);
106+
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, true);
107+
}
121108

122-
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, false, expectedUa1Message);
123-
// Expected valid UA-2 document because PDF/UA-2 does not require Contents in Link annotations
109+
@Test
110+
public void externalLinkTest() throws IOException, InterruptedException, XMPException {
111+
String sourceHtml = SOURCE_FOLDER + "externalLink.html";
112+
String cmpPdfUa1 = SOURCE_FOLDER + "cmp_externalLinkUa1.pdf";
113+
String cmpPdfUa2 = SOURCE_FOLDER + "cmp_externalLinkUa2.pdf";
114+
String destinationPdfUa1 = DESTINATION_FOLDER + "externalLinkUa1.pdf";
115+
String destinationPdfUa2 = DESTINATION_FOLDER + "externalLinkUa2.pdf";
116+
117+
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, true, null);
124118
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, true);
125119
}
126120

@@ -225,19 +219,14 @@ public void pageBreakAfterAvoidTest() throws IOException, InterruptedException,
225219
@Test
226220
// TODO DEVSIX-8864 PDF 2.0: Destination in GoTo action is not a structure destination
227221
// TODO DEVSIX-8476 PDF 2.0 doesn't allow P tag be a child of H tag
228-
// TODO DEVSIX-8679 Apply links alternate description according to UA standard
229222
public void linkWithPageBreakBeforeTest() throws IOException, InterruptedException, XMPException {
230223
String sourceHtml = SOURCE_FOLDER + "linkWithPageBreakBefore.html";
231224
String cmpPdfUa1 = SOURCE_FOLDER + "cmp_linkWithPageBreakBeforeUa1.pdf";
232225
String cmpPdfUa2 = SOURCE_FOLDER + "cmp_linkWithPageBreakBeforeUa2.pdf";
233226
String destinationPdfUa1 = DESTINATION_FOLDER + "linkWithPageBreakBeforeUa1.pdf";
234227
String destinationPdfUa2 = DESTINATION_FOLDER + "linkWithPageBreakBeforeUa2.pdf";
235228

236-
String expectedUa1Message = MessageFormatUtil.format(
237-
PdfUAExceptionMessageConstants.ANNOTATION_OF_TYPE_0_SHOULD_HAVE_CONTENTS_OR_ALT_KEY,
238-
PdfName.Link.getValue());
239-
240-
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, false, expectedUa1Message);
229+
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, true, null);
241230
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, false);
242231
}
243232

0 commit comments

Comments
 (0)