Skip to content

Commit e2d73de

Browse files
author
Dmitry Radchuk
committed
Add overload for PdfAction.CreateURI
DEVSIX-8364
1 parent 5b45cde commit e2d73de

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

io/src/main/java/com/itextpdf/io/util/UrlUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public static URI toNormalizedURI(File file) {
7676
return file.toURI().normalize();
7777
}
7878

79+
/**
80+
* Get the entire URI string which is properly encoded.
81+
*
82+
* @param uri URI which convert to string
83+
* @return URI string representation
84+
*/
85+
public static String toAbsoluteURI(URI uri) {
86+
return uri.toString();
87+
}
88+
7989
public static InputStream openStream(URL url) throws IOException {
8090
return url.openStream();
8191
}

io/src/test/java/com/itextpdf/io/util/UrlUtilTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ This file is part of the iText (R) project.
2929
import java.io.File;
3030
import java.io.IOException;
3131
import java.io.InputStream;
32+
import java.net.URI;
33+
import java.net.URISyntaxException;
3234
import java.net.URL;
3335
import java.net.URLConnection;
3436
import java.net.UnknownHostException;
@@ -109,6 +111,12 @@ public void nullBaseUriTest() throws IOException {
109111
Assert.assertEquals(expected, FileUtil.getParentDirectoryUri(tempFile));
110112
}
111113

114+
@Test
115+
public void toAbsoluteUriTest() throws IOException, URISyntaxException {
116+
String expected = "http://itextpdf.com/";
117+
Assert.assertEquals(expected, UrlUtil.toAbsoluteURI(new URI(expected)));
118+
}
119+
112120
@Test
113121
public void openStreamTest() throws IOException {
114122
String resPath = "./src/test/resources/com/itextpdf/io/util/textFile.dat";

kernel/src/main/java/com/itextpdf/kernel/pdf/action/PdfAction.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.kernel.pdf.action;
2424

2525
import com.itextpdf.io.logs.IoLogMessageConstant;
26+
import com.itextpdf.io.util.UrlUtil;
2627
import com.itextpdf.kernel.exceptions.PdfException;
2728
import com.itextpdf.kernel.pdf.PdfArray;
2829
import com.itextpdf.kernel.pdf.PdfBoolean;
@@ -44,6 +45,7 @@ This file is part of the iText (R) project.
4445
import com.itextpdf.kernel.pdf.navigation.PdfStructureDestination;
4546
import org.slf4j.LoggerFactory;
4647

48+
import java.net.URI;
4749
import java.util.List;
4850

4951
/**
@@ -129,6 +131,16 @@ public PdfAction(PdfDictionary pdfObject) {
129131
markObjectAsIndirect(getPdfObject());
130132
}
131133

134+
/**
135+
* Creates a GoTo action (section 12.6.4.2 of ISO 32000-1) via a given uri.
136+
*
137+
* @param uri the uniform resource identifier to resolve
138+
* @return created action
139+
*/
140+
public static PdfAction createURI(URI uri) {
141+
return createURI(UrlUtil.toAbsoluteURI(uri));
142+
}
143+
132144
/**
133145
* Creates a GoTo action (section 12.6.4.2 of ISO 32000-1) via a given destination.
134146
*

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfActionTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This file is part of the iText (R) project.
3636

3737
import java.io.ByteArrayOutputStream;
3838
import java.io.InputStream;
39+
import java.net.URI;
3940
import java.util.ArrayList;
4041
import java.util.List;
4142
import org.junit.AfterClass;
@@ -82,6 +83,19 @@ public void actionTest02() throws Exception {
8283
System.out.println(MessageFormatUtil.format("Please open document {0} at page 2 and make sure that you're automatically redirected to {1} site.", destinationFolder + "actionTest02.pdf", "http://itextpdf.com"));
8384
}
8485

86+
@Test
87+
public void actionTest03() throws Exception {
88+
PdfDocument document = createDocument(CompareTool.createTestPdfWriter(destinationFolder + "actionTest03.pdf"), true);
89+
String uri = "http://itextpdf.com/";
90+
91+
document.getCatalog().setOpenAction(PdfAction.createURI(new URI(uri)));
92+
Assert.assertEquals(new PdfString(uri),
93+
document.getCatalog().getPdfObject().getAsDictionary(PdfName.OpenAction).get(PdfName.URI));
94+
document.close();
95+
96+
System.out.println(MessageFormatUtil.format("Please open document {0} and make sure that you're automatically redirected to {1} site.", destinationFolder + "actionTest01.pdf", "http://itextpdf.com"));
97+
}
98+
8599
@Test
86100
public void soundActionTest() throws Exception {
87101
String fileName = "soundActionTest.pdf";

0 commit comments

Comments
 (0)