javajpg转pdf的简单介绍
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享javajpg转pdf的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
如何使用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 数据库中2进制流image转成PDF
FileUtils.writeByteArrayToFile(new File("xx.pdf"),p.getBytes());
一般存到数据库的二进制流都是经过加密的,常用的是base64
byte[]bytes = new BASE64Decoder().decodeBuffer(p);
FileUtils.writeByteArrayToFile(new File("xx.pdf"),bytes);

wps支持linux下java文档转换成pdf吗
你好,是支持的,WPS2019版本有正式的linux版本,将word转成pdf的功能,永久免费,稳定不乱
1、打开所需转换的文档
2、点击“特色应用”--“输出为PDF”
javajpg转pdf的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javajpg转pdf的信息别忘了在本站进行查找喔。
