有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Itext Pdf将图像添加到Pdf文件

我正在使用iText生成Pdf。但当我尝试在pdf中添加图像时

Image schoolLogo = Image.getInstance(new URL(timetableResource.getImageUrl())); document.add(schoolLogo);

但我也犯了同样的错误

HTTP Status 500 - Server returned HTTP response code: 400 for URL: http://139.59.72.150:8080/sms/attachments/23/42/school/23/23/Vandana International School Logo.png

type Exception report

message Server returned HTTP response code: 400 for URL:(myUrl)

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.io.IOException: Server returned HTTP response code: 400 for URL: (myUrl) sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) java.net.URL.openStream(URL.java:1045) com.lowagie.text.Image.getInstance(Unknown Source)


共 (3) 个答案

  1. # 2 楼答案

    我已经解决了这个问题,问题是url中的空格。我已经用“% 20”替换空白空间,它工作得非常好。

  2. # 3 楼答案

    请考虑切换到ITXEX7。正如Bruno已经指出的,您当前使用的版本不再受支持。 供您参考,iText7添加图像的方法是:

    String FOX = "path/to/resource/fox.png";
    String DOG = "path/to/resource/dog.png";
    
    Image fox = new Image(ImageDataFactory.create(FOX));
    Image dog = new Image(ImageDataFactory.create(DOG));
    Paragraph p = new Paragraph("The quick brown ")
                    .add(fox)
                    .add(" jumps over the lazy ")
                    .add(dog);
    document.add(p);
    

    有一个完整的jumpstart教程,针对那些已经知道iText如何工作并且需要一些迁移到iText7的指针的人

    http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-1-introducing-basic-building-blocks查看