代码之家  ›  专栏  ›  技术社区  ›  Shaggy Frog

用Java创建PDF缩略图

  •  24
  • Shaggy Frog  · 技术社区  · 16 年前

    我正在寻找一个Java库,可以从第一页中创建一个PDF并创建一个缩略图(PNG)。

    我已经看过jpedal,但它的疯狂许可费是完全禁止的。我现在正在使用IText来操作PDF文件,但我相信它不会生成缩略图。我可以在命令行上使用类似GHESTScript的东西,但是如果可能的话,我希望保持我的项目都是Java。

    2 回复  |  直到 7 年前
        1
  •  22
  •   Simon Sobisch    7 年前

    PDF Renderer 是一个LGPL许可的纯Java库,使其简化为(从他们的示例页面获取):

    File file = new File("test.pdf");
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdffile = new PDFFile(buf);
    
    // draw the first page to an image
    PDFPage page = pdffile.getPage(0);
    
    //get the width and height for the doc at the default zoom 
    Rectangle rect = new Rectangle(0,0,
                    (int)page.getBBox().getWidth(),
                    (int)page.getBBox().getHeight());
    
    //generate the image
    Image img = page.getImage(
                    rect.width, rect.height, //width & height
                    rect, // clip rect
                    null, // null for the ImageObserver
                    true, // fill background with white
                    true  // block until drawing is done
                    );
    
        2
  •  5
  •   mark stephens    16 年前

    只要您只使用PDF文件的子集,PDF渲染器就可以了。使用jpod和jpedal,您将为一个活动的和开发的库付费,而不是一个死项目。

    推荐文章