aspose pdf java,Java 使用aspose.pdf将多张图片转成pdf的方法及示例代码

1、Aspose组件下载

2、多张图片转成pdf

1) 验证license/**

* 获取license

*

* @return

*/

public static boolean getLicense() {

boolean result = false;

try {

InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml");

License aposeLic = new License();

aposeLic.setLicense(is);

result = true;

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

2) 实现代码public static void convertImageToPdf(ArrayList inputImgPaths, String outputFileName) throws Exception {

// Instantiate Document Object

try {

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

for (int i = 0; i < inputImgPaths.size(); i++)

//

{

// Add a page to pages collection of document

Page page = doc.getPages().add();

// Load the source image file to Stream object

java.io.FileInputStream fs = new java.io.FileInputStream(inputImgPaths.get(i));

// Set margins so image will fit, etc.

page.getPageInfo().getMargin().setBottom(0);

page.getPageInfo().getMargin().setTop(0);

page.getPageInfo().getMargin().setLeft(0);

page.getPageInfo().getMargin().setRight(0);

page.setCropBox(new Rectangle(0, 0, 400, 400));

// Create an image object

Image image1 = new Image();

// Add the image into paragraphs collection of the section

page.getParagraphs().add(image1);

// Set the image file stream

image1.setImageStream(fs);

}

doc.save(outputFileName);

} finally {

}

}

或者public static void convertImageToPdf(ArrayList inputImgPaths, String outputFileName) throws Exception {

// Instantiate Document Object

try {

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

for (int i = 0; i < inputImgPaths.size(); i++) {

// Add a page to pages collection of document

Page page = doc.getPages().add();

// create image instance

Image image1 = new Image();

// create BufferedImage instance

java.awt.image.BufferedImage bufferedImage = ImageIO.read(new File(inputImgPaths.get(i)));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// write buffered Image to OutputStream instance

ImageIO.write(bufferedImage, "gif", baos);

baos.flush();

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

// add image to paragraphs collection of first page

page.getParagraphs().add(image1);

// set image stream as OutputStream holding Buffered image

image1.setImageStream(bais);

}

doc.save(outputFileName);

} finally {

}

}

相关文档:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值