如何使用Java在PDF文档中生成页面的缩略图图像?PDF页面的缩略图(Java)
回答
我想http://pdfbox.apache.org/会做你要找的内容,因为你可以从页面创建一个图像,然后缩放图像
从他们的示例代码 -
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pdfbox;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.List;
import org.apache.pdfbox.exceptions.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.util.PDFImageWriter;
/**
* Convert a PDF document to an image.
*
* @author <a href="[email protected]">Ben Litchfield</a>
* @version $Revision: 1.6 $
*/
public class PDFToImage
{
private static final String PASSWORD = "-password";
private static final String START_PAGE = "-startPage";
private static final String END_PAGE = "-endPage";
private static final String IMAGE_FORMAT = "-imageType";
private static final String OUTPUT_PREFIX = "-outputPrefix";
private static final String COLOR = "-color";
private static final String RESOLUTION = "-resolution";
private static final String CROPBOX = "-cropbox";
/**
* private constructor.
*/
private PDFToImage()
{
//static class
}
/**
* Infamous main method.
*
* @param args Command line arguments, should be one and a reference to a file.
*
* @throws Exception If there is an error parsing the document.
*/
public static void main(String[] args) throws Exception
{
String password = "";
String pdfFile = null;
String outputPrefix = null;
String imageFormat = "jpg";
int startPage = 1;
int endPage = Integer.MAX_VALUE;
String color = "rgb";
int resolution;
float cropBoxLowerLeftX = 0;
float cropBoxLowerLeftY = 0;
float cropBoxUpperRightX = 0;
float cropBoxUpperRightY = 0;
try
{
resolution = Toolkit.getDefaultToolkit().getScreenResolution();
}
catch(HeadlessException e)
{
resolution = 96;
}
for(int i = 0; i < args.length; i++)
{
if(args[i].equals(PASSWORD))
{
i++;
if(i >= args.length)
{
usage();
}
password = args[i];
}
else if(args[i].equals(START_PAGE))
{
i++;
if(i >= args.length)
{
usage();
}
startPage = Integer.parseInt(args[i]);
}
else if(args[i].equals(END_PAGE))
{
i++;
if(i >= args.length)
{
usage();
}
endPage = Integer.parseInt(args[i]);
}
else if(args[i].equals(IMAGE_FORMAT))
{
i++;
imageFormat = args[i];
}
else if(args[i].equals(OUTPUT_PREFIX))
{
i++;
outputPrefix = args[i];
}
else if(args[i].equals(COLOR))
{
i++;
color = args[i];
}
else if(args[i].equals(RESOLUTION))
{
i++;
resolution = Integer.parseInt(args[i]);
}
else if(args[i].equals(CROPBOX))
{
i++;
cropBoxLowerLeftX = Float.valueOf(args[i]).floatValue();
i++;
cropBoxLowerLeftY = Float.valueOf(args[i]).floatValue();
i++;
cropBoxUpperRightX = Float.valueOf(args[i]).floatValue();
i++;
cropBoxUpperRightY = Float.valueOf(args[i]).floatValue();
}
else
{
if(pdfFile == null)
{
pdfFile = args[i];
}
}
}
if(pdfFile == null)
{
usage();
}
else
{
if(outputPrefix == null)
{
outputPrefix = pdfFile.substring(0, pdfFile.lastIndexOf('.'));
}
PDDocument document = null;
try
{
document = PDDocument.load(pdfFile);
//document.print();
if(document.isEncrypted())
{
try
{
document.decrypt(password);
}
catch(InvalidPasswordException e)
{
if(args.length == 4)//they supplied the wrong password
{
System.err.println("Error: The supplied password is incorrect.");
System.exit(2);
}
else
{
//they didn't supply a password and the default of "" was wrong.
System.err.println("Error: The document is encrypted.");
usage();
}
}
}
int imageType = 24;
if ("bilevel".equalsIgnoreCase(color))
{
imageType = BufferedImage.TYPE_BYTE_BINARY;
}
else if ("indexed".equalsIgnoreCase(color))
{
imageType = BufferedImage.TYPE_BYTE_INDEXED;
}
else if ("gray".equalsIgnoreCase(color))
{
imageType = BufferedImage.TYPE_BYTE_GRAY;
}
else if ("rgb".equalsIgnoreCase(color))
{
imageType = BufferedImage.TYPE_INT_RGB;
}
else if ("rgba".equalsIgnoreCase(color))
{
imageType = BufferedImage.TYPE_INT_ARGB;
}
else
{
System.err.println("Error: the number of bits per pixel must be 1, 8 or 24.");
System.exit(2);
}
//si une cropBox a ete specifier, appeler la methode de modification de cropbox
//changeCropBoxes(PDDocument document,float a, float b, float c,float d)
if (cropBoxLowerLeftX!=0 || cropBoxLowerLeftY!=0 || cropBoxUpperRightX!=0 || cropBoxUpperRightY!=0)
{
changeCropBoxes(document,cropBoxLowerLeftX, cropBoxLowerLeftY, cropBoxUpperRightX, cropBoxUpperRightY);
}
//Make the call
PDFImageWriter imageWriter = new PDFImageWriter();
boolean success = imageWriter.writeImage(document, imageFormat, password,
startPage, endPage, outputPrefix, imageType, resolution);
if (!success)
{
System.err.println("Error: no writer found for image format '"
+ imageFormat + "'");
System.exit(1);
}
}
catch (Exception e)
{
System.err.println(e);
}
finally
{
if(document != null)
{
document.close();
}
}
}
}
/**
* This will print the usage requirements and exit.
*/
private static void usage()
{
System.err.println("Usage: java org.apache.pdfbox.PDFToImage [OPTIONS] <PDF file>\n" +
" -password <password> Password to decrypt document\n" +
" -imageType <image type> (" + getImageFormats() + ")\n" +
" -outputPrefix <output prefix> Filename prefix for image files\n" +
" -startPage <number> The first page to start extraction(1 based)\n" +
" -endPage <number> The last page to extract(inclusive)\n" +
" -color <string> The color depth (valid: bilevel, indexed, gray, rgb, rgba)\n" +
" -resolution <number> The bitmap resolution in dpi\n" +
" -cropbox <number> <number> <number> <number> The page area to export\n" +
" <PDF file> The PDF document to use\n"
);
System.exit(1);
}
private static String getImageFormats()
{
StringBuffer retval = new StringBuffer();
String[] formats = ImageIO.getReaderFormatNames();
for(int i = 0; i < formats.length; i++)
{
retval.append(formats[i]);
if(i + 1 < formats.length)
{
retval.append(",");
}
}
return retval.toString();
}
private static void changeCropBoxes(PDDocument document,float a, float b, float c,float d)
{
List pages = document.getDocumentCatalog().getAllPages();
for(int i = 0; i < pages.size(); i++)
{
System.out.println("resizing page");
PDPage page = (PDPage)pages.get(i);
PDRectangle rectangle = new PDRectangle();
rectangle.setLowerLeftX(a);
rectangle.setLowerLeftY(b);
rectangle.setUpperRightX(c);
rectangle.setUpperRightY(d);
page.setMediaBox(rectangle);
page.setCropBox(rectangle);
}
}
}
它也保留阿拉伯文字符,因为我有这个问题。 http://stackoverflow.com/questions/16665300/setting-ttf-for-pdf-to-image-on-pdfbox – Genjuro 2013-05-22 08:47:37
使用2.x版本的pdfbox生成图像,请参阅http://stackoverflow.com/a/ 23327024/580021 – Jollyjagga 2017-05-22 12:47:52
你也可以有看看JPedal(详细信息在http://www.jpedal.org/pdf_thumbnail.php)
IcePdf是我见过的最好的(免费)阅读pdf。 JPedal很棒,但不是免费的。
如果您打算从普通大众可以向您发送的PDF中生成图像,我向您保证(您将从经验中获得)会导致JVM崩溃的pdf。 (即:如果它们是具有所有矢量图形的多层pdf)。 This pdf是一个会使许多库崩溃的例子(但它是一个完全有效的PDF,没有像JavaScript这样有趣的东西等)。
我们已经走下了试图使用大量库的路线,并最终将创建缩略图的工作委托给ImageMagick,ImageMagick是一个高度优化的用于图像处理的C程序。
JPedal和Icepdf都有LGPL和具有不同功能集的商业版本 - 在这两种情况下,LGPL版本都具有较少的功能。还有PDF-Renderer,但不支持所有PDF文件。 – 2010-12-09 14:02:54
This post不仅是PDF格式,但也有许多其他的文件类型,如办公,图像,文本....
- 1. 生成PDF页面缩略图
- 2. 动态页面缩略图
- 3. phpThumb PDF缩略图
- 4. 如何为PDF文件的某些页面生成缩略图?
- 5. 如何快速生成PDF所有页面的PNG缩略图?
- 6. 使用itextsharp获取PDF页面的缩略图
- 7. 如何创建第一个PDF页面的缩略图与carrierwave
- 8. iOS在PDF中嵌入页面缩略图
- 9. PDF缩略图未显示
- 10. PDF缩略图显示
- 11. 为PDF生成缩略图
- 12. ghostscript pdf至缩略图
- 13. 缩略图url返回页面顶部
- 14. 在Android中创建PDF的缩略图
- 15. TeXShop(LaTeX)中的PDF缩略图
- 16. 使用PDF.js生成PDF的缩略图
- 17. WordPress - 显示子页面(标题和缩略图)ON子页面
- 18. 为什么Google会忽略我的+1页面缩略图?
- 19. 将PDF转换为Java中的缩略图
- 20. 在浏览器中显示PDF - 默认显示缩略图(“页面”)
- 21. 分页缩略图与kaminari
- 22. 分页缩略图PHP
- 23. 使用php显示缩略图pdf
- 24. pdf缩略图imagemagick php不工作
- 25. PDF缩略图shell命令问题 - PHP
- 26. PDF缩略图不能通过EXEC
- 27. 缩小pdf页面的内容
- 28. 如何生成HTML页面的缩略图图像
- 29. YouTube v3中的缩略图缩略图
- 30. PDF压缩java
看看这个[文章](http://stackoverflow.com/questions/4929813/convert- PDF到缩略图像中的Java/4930488#4930488)。 – sdorra 2011-02-08 10:59:29