導航:首頁 > 文檔加密 > 圖片轉pdfjava

圖片轉pdfjava

發布時間:2022-11-02 17:44:03

java pdf轉圖片問題

搜索添加spire.pdf.jar文件為依賴,pdf轉圖片代碼如下:

  1. import java.awt.image.BufferedImage;

  2. import java.io.File;

  3. import java.io.IOException;

  4. import com.spire.pdf.PdfDocument;

  5. import javax.imageio.ImageIO;


  6. public class toImage {


  7. public static void main(String[] args) throws IOException {

  8. //載入PDF文件

  9. PdfDocument doc = new PdfDocument();

  10. doc.loadFromFile("Sample.pdf");

  11. //保存PDF的每一頁到圖片

  12. BufferedImage image;

  13. for (int i = 0; i < doc.getPages().getCount(); i++) {

  14. image = doc.saveAsImage(i);

  15. File file = new File( String.format("ToImage-img-%d.png", i));

  16. ImageIO.write(image, "PNG", file);

  17. }

  18. doc.close();

  19. }

  20. }

Ⅱ Java中將Gif圖片格式化為PDF

我也想轉,用itext可以把它圍成一個pdf文件,但是我想直接把接收到的gif二進制轉成pdf不經過圖片。你那個實現了沒有?

Ⅲ 如何使用java將cgm轉換成pdf文件

總結對jacob和Itext學習總結.本文試驗的是將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可以,找下案例,可以繼續追問。

閱讀全文

與圖片轉pdfjava相關的資料

熱點內容
ace程序員指南源碼 瀏覽:414
哪個app可以分36期還款 瀏覽:243
為什麼安卓軟體平台那麼多 瀏覽:286
如果把伺服器切斷了怎麼辦 瀏覽:17
app建立網站需要什麼技術 瀏覽:689
迷你世界教你開雲伺服器 瀏覽:560
鋁窗下料用什麼app 瀏覽:842
天堂伺服器怎麼進 瀏覽:385
程序員汽車號碼 瀏覽:461
飛度空調壓縮機可以換大嗎 瀏覽:337
紫色系解壓視頻教程 瀏覽:359
與門單片機 瀏覽:712
正規的重慶移動伺服器租用雲主機 瀏覽:71
php清除所有session 瀏覽:728
如何編譯ts 瀏覽:798
學生買個雲伺服器要多少錢 瀏覽:999
在運行對話框中輸入命令提示符 瀏覽:292
微信公眾號開發教程php 瀏覽:198
電腦作為伺服器怎麼配置 瀏覽:96
磨砂殼文件夾 瀏覽:985