Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1110: Images examples in pdf-toolbox fails to find the images #1111

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,21 @@ public static Image getInstance(String filename)
return getInstance(Utilities.toURL(filename));
}

/**
* Gets an instance of an Image from the classpath.
*
* @param filename a filename
* @return an object of type <CODE>Gif</CODE>,<CODE>Jpeg</CODE> or
* <CODE>Png</CODE>
* @throws BadElementException if error in creating {@link ImgWMF#ImgWMF(byte[]) ImgWMF}
* @throws IOException if image is not recognized
*/
public static Image getInstanceFromClasspath(String filename)
throws BadElementException, IOException {
URL url = Image.class.getResource("/" + filename);
return getInstance(url);
}

/**
* gets an instance of an Image
*
Expand Down
7 changes: 7 additions & 0 deletions openpdf/src/test/java/com/lowagie/text/ImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ void shouldReturnImageWithUrlForPath() throws Exception {
assertNotNull(image.getUrl());
}

@Test
void shouldReturnImageWithUrlFromClasspath() throws Exception {
String fileName = "H.gif";
final Image image = Image.getInstanceFromClasspath(fileName);
assertNotNull(image.getUrl());
}

@Test
void shouldReturnImageWithoutUrl() throws IOException {
byte[] imageBytes = readFileBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ public static void main(String[] args) {

// step 4:
document.add(new Paragraph("A picture of my dog: otsoe.jpg"));
Image jpg = Image.getInstance("otsoe.jpg");
Image jpg = Image.getInstanceFromClasspath("otsoe.jpg");
document.add(jpg);
document.add(new Paragraph("getacro.gif"));
Image gif = Image.getInstance("getacro.gif");
Image gif = Image.getInstanceFromClasspath("getacro.gif");
document.add(gif);
document.add(new Paragraph("pngnow.png"));
Image png = Image.getInstance("pngnow.png");
Image png = Image.getInstanceFromClasspath("pngnow.png");
document.add(png);
document.add(new Paragraph("grayscaled.png"));
Image grayscaledPng = Image.getInstance("grayscaled.png");
Image grayscaledPng = Image.getInstanceFromClasspath("grayscaled.png");
document.add(grayscaledPng);
document.add(new Paragraph("iText.bmp"));
Image bmp = Image.getInstance("iText.bmp");
Image bmp = Image.getInstanceFromClasspath("iText.bmp");
document.add(bmp);
document.add(new Paragraph("iText.wmf"));
Image wmf = Image.getInstance("iText.wmf");
Image wmf = Image.getInstanceFromClasspath("iText.wmf");
document.add(wmf);
document.add(new Paragraph("iText.tif"));
Image tif = Image.getInstance("iText.tif");
Image tif = Image.getInstanceFromClasspath("iText.tif");
document.add(tif);
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
Expand Down
Loading