如何使用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