如何使用itext添加图像并设计页眉,页脚为pdf? 我写过这个,但没有找到异常文件。使用itext将图像文件插入PDF时找不到文件
Image image = Image.getInstance("\resources\image.gif");
感谢
如何使用itext添加图像并设计页眉,页脚为pdf? 我写过这个,但没有找到异常文件。使用itext将图像文件插入PDF时找不到文件
Image image = Image.getInstance("\resources\image.gif");
感谢
看看这个example
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class CreatePDF{
public static void main(String arg[])throws Exception{
try{
Document document=new Document();
FileOutputStream fos=new FileOutputStream("C:/header-footer.pdf");
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
Image image1 = Image.getInstance("C:/image1.jpg");
Image image2 = Image.getInstance("C:/image2.jpg");
image1.setAbsolutePosition(0, 0);
image2.setAbsolutePosition(0, 0);
PdfContentByte byte1 = writer.getDirectContent();
PdfTemplate tp1 = byte1.createTemplate(600, 150);
tp1.addImage(image2);
PdfContentByte byte2 = writer.getDirectContent();
PdfTemplate tp2 = byte2.createTemplate(600, 150);
tp2.addImage(image1);
byte1.addTemplate(tp1, 0, 715);
byte2.addTemplate(tp2, 0, 0);
Phrase phrase1 = new Phrase(byte1 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL));
Phrase phrase2 = new Phrase(byte2 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL));
HeaderFooter header = new HeaderFooter(phrase1, true);
HeaderFooter footer = new HeaderFooter(phrase2, true);
document.setHeader(header);
document.setFooter(footer);
document.close();
System.out.println("File is created successfully showing header and footer.");
}
catch (Exception ex){
System.out.println(ex);
}
}
}
尝试OD,由我怎样才能设置图片的方式这是在项目投产/resources/images/.jpg? – sudo 2012-01-17 10:52:01
通过使用从驱动器名称开始的绝对路径或使用相对路径(取决于您从何处开始搜索)而枯萎。 – CloudyMarble 2012-01-17 11:03:56
我用下面的代码从类路径中插入图像。当您需要包含无法从公开网址访问的图片时通常很有用。
Image img = Image.getInstance(getClass().getClassLoader().getResource("MyImage.jpg"));
在我的情况,我用maven,所以我把MyImage.jpg
中的src/main /资源
绝对PAH – 2012-01-17 08:58:17