当前位置: 首页 > 工具软件 > ICEpdf > 使用案例 >

ICEpdf把pdf转成图片

令狐高洁
2023-12-01

背景:要求手机能在线预览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);
  }
}



 类似资料: