PDF页面的缩略图(Java)
如何使用Java生成PDF文档中页面的缩略图?我想这会满足您的要求,因为您可以从页面创建图像,然后缩放图像 从他们的示例代码中-PDF页面的缩略图(Java),java,pdf,thumbnails,Java,Pdf,Thumbnails,如何使用Java生成PDF文档中页面的缩略图?我想这会满足您的要求,因为您可以从页面创建图像,然后缩放图像 从他们的示例代码中- /* * 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
/*
* 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="ben@benlitchfield.com">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);
}
}
}
/*
*根据一个或多个许可证颁发给Apache软件基金会(ASF)
*贡献者许可协议。请参阅随附的通知文件
*本作品提供了有关版权所有权的更多信息。
*ASF根据Apache许可证2.0版将此文件许可给您
*(以下简称“许可证”);除非符合以下要求,否则不得使用此文件
*执照。您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
包org.apache.pdfbox;
导入java.awt.HeadlessException;
导入java.awt.Toolkit;
导入java.awt.image.buffereImage;
导入javax.imageio.imageio;
导入java.util.List;
导入org.apache.pdfbox.exceptions.InvalidPasswordException;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.pdmodel.PDPage;
导入org.apache.pdfbox.pdmodel.common.PDRectangle;
导入org.apache.pdfbox.util.PDFImageWriter;
/**
*将PDF文档转换为图像。
*
*@作者
*@version$Revision:1.6$
*/
公共类PDFToImage
{
私有静态最终字符串PASSWORD=“-PASSWORD”;
私有静态最终字符串开始页面=“-startPage”;
私有静态最终字符串END_PAGE=“-endPage”;
私有静态最终字符串IMAGE_FORMAT=“-imageType”;
私有静态最终字符串输出_PREFIX=“-outputPrefix”;
私有静态最终字符串COLOR=“-COLOR”;
私有静态最终字符串解析=“-RESOLUTION”;
私有静态最终字符串CROPBOX=“-CROPBOX”;
/**
*私有构造函数。
*/
私人PDFToImage()
{
//静态类
}
/**
*臭名昭著的主要方法。
*
*@param args命令行参数应为1,并且是对文件的引用。
*
*@在分析文档时出错时引发异常。
*/
公共静态void main(字符串[]args)引发异常
{
字符串密码=”;
字符串pdfFile=null;
字符串outputPrefix=null;
字符串imageFormat=“jpg”;
int起始页=1;
int endPage=Integer.MAX_值;
字符串color=“rgb”;
整数分辨率;
浮动cropBoxLowerLeftX=0;
浮动cropBoxLowerLeftY=0;
float cropBoxUpperRightX=0;
float cropBoxUpperRightY=0;
尝试
{
分辨率=Toolkit.getDefaultToolkit().getScreenResolution();
}
捕捉(无头例外e)
{
分辨率=96;
}
对于(int i=0;i=args.length)
{
用法();
}
密码=args[i];
}
else if(args[i].equals(起始页))
{
i++;
如果(i>=args.length)
{
用法();
}
startPage=Integer.parseInt(args[i]);
}
else if(args[i].equals(END_PAGE))
{
i++;
如果(i>=args.length)
{
用法();
}
endPage=Integer.parseInt(args[i]);
}
else if(args[i].equals(IMAGE_格式))
{
i++;
imageFormat=args[i];
}
else if(args[i].equals(输出前缀))
{
i++;
outputPrefix=args[i];
}
else if(args[i].equals(COLOR))
{
i++;
颜色=args[i];
}
else if(args[i].等于(分辨率))
{
i++;
分辨率=整数.parseInt(args[i]);
}
else如果(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();
}
其他的
{
如果(Pdfile==null)
{
Pdfile=args[i];
}
}
}
如果(Pdfile==null)
{
用法();
}
其他的
{
if(outputPrefix==null)
{
outputPrefix=pdfFile.substring(0,pdfFile.lastIndexOf('.');
}
PDDocument文件=null;
尝试
{
document=PDDocument.load(pdfFile);
//document.print();
if(document.isEncrypted())
{
尝试
{
文件。解密(密码);
}
捕获(InvalidPasswordException e)
{
if(args.length==4)//他们提供了错误的密码
{
System.err.println(“错误:提供的密码不正确。”);
系统出口(2);
}
其他的