搜索添加spire.pdf.jar文件為依賴,pdf轉圖片代碼如下:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.spire.pdf.PdfDocument;
import javax.imageio.ImageIO;
public class toImage {
public static void main(String[] args) throws IOException {
//載入PDF文件
PdfDocument doc = new PdfDocument();
doc.loadFromFile("Sample.pdf");
//保存PDF的每一頁到圖片
BufferedImage image;
for (int i = 0; i < doc.getPages().getCount(); i++) {
image = doc.saveAsImage(i);
File file = new File( String.format("ToImage-img-%d.png", i));
ImageIO.write(image, "PNG", file);
}
doc.close();
}
}
Ⅱ Java中將Gif圖片格式化為PDF
我也想轉,用itext可以把它圍成一個pdf文件,但是我想直接把接收到的gif二進制轉成pdf不經過圖片。你那個實現了沒有?
Ⅲ 如何使用java將cgm轉換成pdf文件
一、先將cgm轉換成HMTL文件格式
二、用流讀取HTML文件。將其保存在一個String對象中。
三、用Itext組件,將生成的字元串對象轉換成PDF文件。
四、在要生成的PDF文件加入所需信息。
在此:有幾點問題如還請前輩解答:1、怎麼控制我在PDF文件加入某段文字的字體、大小、間距等。
/**
* 生成PDF文件
* @author 於學明
*
*/
public class CreatePdf {
/**
* 獲得PDF文件所需圖片
* @param imagePath //圖片文件路徑
* @return
* @throws BadElementException
* @throws MalformedURLException
* @throws IOException
*/
public Image getImageFile(String imagePath) throws BadElementException, MalformedURLException, IOException{
Image jpg = Image.getInstance(imagePath);
//設置圖片居中
jpg.setAlignment(Image.MIDDLE);
return jpg;
}
/**
* 獲得文字內容
* @param inputFilePath 原DOC文件路徑
* @param outputFilePath 生成HTML文件路徑
* @return
*/
public String getPdfContext(String inputFilePath,String outputFilePath){
//讀取DOC文件內容
String htmlText = new FileExtracter().extractDoc(inputFilePath, outputFilePath);
//把讀取的HTML文件,生成一個字元串
String pdf = new FileExtracter().getContext(htmlText);
return pdf;
}
/**
* 用ITEXT生成指定PDF格式文件
* @param imagePath0
* @param inputFilePath
* @param outputFilePath
* @param imagePath1
* @param outputPdf
* @return
* @throws DocumentException
* @throws IOException
*/
public String createPDF(String imagePath0,String inputFilePath,String outputFilePath,String imagePath1,String outputPdf) throws DocumentException, IOException{
//返回的pdf全路徑
String returnPdf="";
File dir=new File("out_pdf");
//若目錄不存在則新建該目錄
if(!dir.exists()){
dir.mkdir();
}
//新建空白文件
File outPdfPath=new File(dir+"/"+outputPdf);//輸出pdf文件的全路徑
try {
outPdfPath.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
returnPdf=null;
}
//定義PDF文件大小和邊距
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//生成PDF文件的路徑
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outPdfPath));
writer.setViewerPreferences(PdfWriter.PageModeFullScreen);
document.open();
//文件頭圖片
document.add(getImageFile(imagePath0));
//定義字體,可以正常顯示中文
BaseFont bfComic = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 12, Font.NORMAL);
String pdf = getPdfContext(inputFilePath, outputFilePath);
//String str=new String(pdf.getBytes("ISO-8859-1"),"GB2312");
document.add(new Paragraph(pdf,font));
//文件尾圖片
document.add(getImageFile(imagePath1));
document.close();
returnPdf = outPdfPath.getAbsolutePath();
return returnPdf;
}
/**
* 用ITEXT生成指定PDF格式文件
* @param imagePath
* @param inputFilePath
* @param outputFilePath
* @param outputPdf
* @return
* @throws DocumentException
* @throws IOException
*/
public String createPDF(String imagePath,String inputFilePath,String outputFilePath,String outputPdf) throws DocumentException, IOException{
//返回的pdf全路徑
String returnPdf="";
File dir=new File("out_pdf");
//若目錄不存在則新建該目錄
if(!dir.exists()){
dir.mkdir();
}
//新建空白文件
File outPdfPath=new File(dir+"/"+outputPdf);//輸出pdf文件的全路徑
try {
outPdfPath.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
returnPdf=null;
}
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//生成PDF文件的路徑
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outPdfPath));
writer.setViewerPreferences(PdfWriter.PageModeFullScreen);
document.open();
document.add(getImageFile(imagePath));
//定義字體,可以正常顯示中文
BaseFont bfComic = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 12, Font.NORMAL);
String pdf = getPdfContext(inputFilePath, outputFilePath);
//String str=new String(pdf.getBytes("ISO-8859-1"),"GB2312");
document.add(new Paragraph(pdf,font));
document.close();
returnPdf = outPdfPath.getAbsolutePath();
return returnPdf;
}
public static void main(String [] args){
try {
String s = new CreatePdf().createPDF("c:/a.gif","c:/s.doc", "c:/x.html", "a.pdf");
System.out.println(s);
} catch (DocumentException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
Ⅳ java生成pdf,圖片怎麼導不進去
不知道你用的什麼方法來導的,但是如果通過PDF類庫jar包來實現的話,應該是沒問題的,參考如下java代碼中關於如何插入圖片到PDF的方法:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
public class AddImage {
public static void main(String[] args) {
//創建文檔
PdfDocument pdf = new PdfDocument();
//添加一頁
PdfPageBase page = pdf.getPages().add();
//載入圖片,並獲取圖片高寬
PdfImage image = PdfImage.fromFile("fj.png");
int width = image.getWidth()/2;
int height = image.getHeight()/2;
//繪制圖片到PDF
page.getCanvas().drawImage(image,50,50,width, height);
//保存文檔
pdf.saveToFile("result.pdf");
pdf.dispose();
}}
Ⅳ java EMF轉為PNG或者PDF
用虛擬列印機,軟體有: 1.PDFFactory Pro虛擬列印機,安裝後,在任何文檔中,選擇列印時,選擇列印機為pdfFactoryPro,就能生成PDF文件,並可以進行安全設置。 2.SmartPrinter(Doc Pdf xls to pdf/tiff/bmp/jpg/png)一款大家非常熟悉的經典產品,專為轉換文件而研發的高品質列印驅動,以運行穩定、轉換速度快和圖像質量高而著稱,通過虛擬列印技術可以完美的將任意可列印文檔轉換成 PDF、TIFF、JPEG,BMP、PNG、EMF、GIF、TXT格式。 3.雪瑩DocConvert虛擬列印轉換。雪瑩DocConvert是一款文檔轉化工具,它通過虛擬列印的技術將任何文檔轉化為PDF,JPG,BMP,TIFF,PCX,PNG等等文檔格式。
Ⅵ java 將圖片生成pdf 問題
不知道你用什麼方法生成 的,你可以去網路搜索 java IText 用那個生成PDF幾句代碼就行了
網路有現成的例子
大約是。。。他可以直接把圖片,生成PDF
Document doc = new Document(null, 0, 0, 0, 0);
Image image = Image.getInstance(imgPath);
PdfWriter.getInstance(doc, fos);
Ⅶ java 帶圖片的word怎麼轉成pdf
首先讀取word 內容 然後再寫到pdf文件中。提示的很清楚了,請不要不自己動腦思考,拒絕伸手黨!
Ⅷ java 圖片流生成pdf
你是想在java生成一個pdf文件?並通過流輸出?iText可以,找下案例,可以繼續追問。