主要应用于PDF转打印,图片切换等
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.PDFStreamEngine;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
/**
* This is an example on how to extract images from pdf.
*/
public class SaveImagesInPdf extends PDFStreamEngine
{
/**
* Default constructor.
*
* @throws IOException If there is an error loading text stripper properties.
*/
public SaveImagesInPdf() throws IOException
{
}
public int imageNumber = 1;
/**
* @param args The command line arguments.
*
* @throws IOException If there is an error parsing the document.
*/
public static void main( String[] args ) throws IOException
{
PDDocument document = null;
String fileName = "test.pdf";
try
{
document = PDDocument.load( new File(fileName) );
SaveImagesInPdf printer = new SaveImagesInPdf();
int pageNum = 0;
for( PDPage page : document.getPages() )
{
pageNum ;
System.out.println( "Processing page: " pageNum );
printer.processPage(page);
}
}
finally
{
if( document != null )
{
document.close();
}
}
}
/**
* @param operator The operation to perform.
* @param operands The list of arguments.
*
* @throws IOException If there is an error processing the operation.
*/
@Override
protected void processOperator( Operator operator, List<COSBase> operands) throws IOException
{
String operation = operator.getName();
if( "Do".equals(operation) )
{
COSName objectName = (COSName) operands.get( 0 );
PDXObject xobject = getResources().getXObject( objectName );
if( xobject instanceof PDImageXObject)
{
PDImageXObject image = (PDImageXObject)xobject;
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// same image to local
BufferedImage bImage = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_ARGB);
bImage = image.getImage();
ImageIO.write(bImage,"PNG",new File("image_" imageNumber ".png"));
System.out.println("Image saved.");
imageNumber ;
}
else if(xobject instanceof PDFormXObject)
{
PDFormXObject form = (PDFormXObject)xobject;
showForm(form);
}
}
else
{
super.processOperator( operator, operands);
}
}
}
资源文件列表
getPDFElementLocations/.classpath , 648
getPDFElementLocations/.project , 398
getPDFElementLocations/bin/PrintImageLocations.class , 5642
getPDFElementLocations/bin/PrintTextLocations.class , 3629
getPDFElementLocations/bin/SaveImagesInPdf.class , 4132
getPDFElementLocations/bin/test.pdf , 228429
getPDFElementLocations/image_1.png , 1892
getPDFElementLocations/image_2.png , 1892
getPDFElementLocations/image_3.png , 1892
getPDFElementLocations/image_4.png , 1892
getPDFElementLocations/image_5.png , 1892
getPDFElementLocations/libs/commons-logging-1.2.jar , 61829
getPDFElementLocations/libs/fontbox-2.0.19.jar , 1561265
getPDFElementLocations/libs/pdfbox-2.0.19.jar , 2715618
getPDFElementLocations/libs/pdfbox-tools-2.0.19.jar , 81206
getPDFElementLocations/src/PrintImageLocations.java , 6351
getPDFElementLocations/src/PrintTextLocations.java , 4039
getPDFElementLocations/src/SaveImagesInPdf.java , 3328
getPDFElementLocations/test.pdf , 228429
getPDFElementLocations/九江十九中2019年下学期语文考试20190927094031.txt , 16769
getPDFElementLocations/图像位置信息.txt , 2021
