ICEpdf把pdf转成图片

背景:要求手机能在线预览pdf文件的内容。

pc的话可以很方便的利用开源的一些js来加载pdf,但是在手机上却行不通,只能变通一下,把pdf转成图片,然后手机按页去请求pdf的内容。

icepdf官网:http://www.icesoft.org/java/downloads/icepdf-downloads.jsf 最新版是6.2.3

public class PdfUtil {
	
	static{
		System.setProperty("org.icepdf.core.streamcache.enabled", "true");
	}
	
	/**
	 * @param page 页码,从0开始
	 * @param pdfContent   pdf文件的字节流
	 * */
	public static PdfPage pdfToImage(int page, byte[] pdfContent){
		if(pdfContent == null){
			throw new RuntimeException("pdf文件内容不能为空");
		}
		ByteArrayInputStream bin = new ByteArrayInputStream(pdfContent);
		return pdfToImage(page, bin);
	}
	/**
	 * @param page 页码,从0开始
	 * @param in   pdf文件的文件流
	 * */
	public static PdfPage pdfToImage(int page, InputStream in){
		Document document = null;
		try{
			document = new Document();
			document.setInputStream(in, null);
			int totalPages = document.getNumberOfPages();
			if(page > totalPages){
				return new PdfPage(page, totalPages, false, null);
			}
			float scale = 1.0f;// 缩放比例
			float rotation = 0f;// 旋转角度
			BufferedImage image = (BufferedImage) document.getPageImage(page, GraphicsRenderingHints.SCREEN,org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			try {
				ImageIO.write(image, "png", out);
			} catch (IOException e) {
				throw new RuntimeException("pdf内容读取异常", e);
			}
			image.flush();
			IOUtil.closeQuietly(out);
			return new PdfPage(page, totalPages, page<totalPages-1, out.toByteArray());
		}finally{
			document.dispose();
			IOUtil.closeQuietly(in);
		}
	}
}
免费使用版式带水印的,参考网上的说法,可以去掉水印。具体来说:

在项目中新建一个org.icepdf.core.pobjects.graphics.Padding的类,然后重写里面的方法就可以了:

public class Padding
{
  private static byte[] padding1 = { 54, 48, 50, 93, 81, 83, 13 };
  private static byte[] padding3 = { 51, 92, 95, 13, 50, 99, 78, 89, 98, 78, 97, 86, 92, 91, 13, 61, 98, 95, 93, 92, 96, 82, 96, 13, 60, 91, 89, 102 };
  
  static{
    for (int i = 0; i < padding1.length; i++)
    {
      int tmp226_225 = i;
      byte[] tmp226_222 = padding1;
      tmp226_222[tmp226_225] = ((byte)(tmp226_222[tmp226_225] + 19));
    }
    for (int i = 0; i < padding3.length; i++)
    {
      int tmp253_252 = i;
      byte[] tmp253_249 = padding3;
      tmp253_249[tmp253_252] = ((byte)(tmp253_249[tmp253_252] + 19));
    }
  }
  
  public static final void getPadding(Graphics2D paramGraphics2D, Rectangle2D.Float paramFloat){
    paramGraphics2D.scale(1.0D, -1.0D);
    paramGraphics2D.setColor(new Color(186, 0, 0));
    /* 这个就是生成水印的地方
    String str1 = new ProductInfo().getVersion();
    String str2 = new String(padding1) + str1;
    String str3 = new String(padding3);
    */
    String str2 = "";
    String str3 = "";
    int i = 5;
    paramGraphics2D.setFont(new Font("Dialog", 1, 14));
    FontMetrics localFontMetrics = paramGraphics2D.getFontMetrics();
    Rectangle2D localRectangle2D = localFontMetrics.getStringBounds(str3.toCharArray(), 0, str3.length(), paramGraphics2D);
    int j = (int)(paramFloat.x + (paramFloat.width - localRectangle2D.getWidth()) / 2.0D);
    int k = -1 * i;
    paramGraphics2D.drawString(str3, j, k);
    localRectangle2D = localFontMetrics.getStringBounds(str2.toCharArray(), 0, str2.length(), paramGraphics2D);
    j = (int)(paramFloat.x + (paramFloat.width - localRectangle2D.getWidth()) / 2.0D);
    k = -1 * (int)(paramFloat.height - localRectangle2D.getHeight() - i);
    paramGraphics2D.drawString(str2, j, k);
  }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值