本文介紹了Java:使用PDFBox 1庫從圖像創建PDF頁面的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要將圖像(主要是JPEG)直接轉換為PDF文檔的PDF頁面。
可能是圖像大小不同。
每個PDF頁面應具有與圖像完全相同的尺寸。
因此,每頁僅包含全分辨率圖像。
將頁面設置為圖像/內容的尺寸,如何實現這一點?
因為我看到的PDF文件具有不同的頁面大小和方向,但如何使用PDFBox做到這一點?
推薦答案
注意(評論者:Valentin Waeselynck:從起,您現在可以使用PDImageXObject.createFromFile
、PDImageXObject.createFromFile
或JPEGFactory.createFromStream
。請注意,LosslessFactory.createFromImage
將大大降低空間效率。請參閱:https://pdfbox.apache.org/2.0/migration.html#working-with-images
PDFBox 1解決方案:
我已經用以下代碼解決了這個問題:
PDDocument document = new PDDocument();
InputStream in = new FileInputStream(someImage);
BufferedImage bimg = ImageIO.read(in);
float width = bimg.getWidth();
float height = bimg.getHeight();
PDPage page = new PDPage(new PDRectangle(width, height));
document.addPage(page);
PDXObjectImage img = new PDJpeg(document, new FileInputStream(someImage));
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(img, 0, 0);
contentStream.close();
in.close();
document.save("test.pdf");
document.close();
為了完整起見,請參閱PDFBox 2+API(https://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java)的當前PDFBox示例中的代碼
/*
* 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.examples.pdmodel;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
/**
* This is an example that creates a reads a document and adds an image to it..
*
* The example is taken from the pdf file format specification.
*
* @author Ben Litchfield
*/
public class AddImageToPDF
{
/**
* Add an image to an existing PDF document.
*
* @param inputFile The input PDF to add the image to.
* @param imagePath The filename of the image to put in the PDF.
* @param outputFile The file to write to the pdf to.
*
* @throws IOException If there is an error writing the data.
*/
public void createPDFFromImage( String inputFile, String imagePath, String outputFile )
throws IOException
{
try (PDDocument doc = PDDocument.load(new File(inputFile)))
{
//we will add the image to the first page.
PDPage page = doc.getPage(0);
// createFromFile is the easiest way with an image file
// if you already have the image in a BufferedImage,
// call LosslessFactory.createFromImage() instead
PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true))
{
// contentStream.drawImage(ximage, 20, 20 );
// better method inspired by http://stackoverflow.com/a/22318681/535646
// reduce this value if the image is too large
float scale = 1f;
contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth() * scale, pdImage.getHeight() * scale);
}
doc.save(outputFile);
}
}
/**
* This will load a PDF document and add a single image on it.
* <br>
* see usage() for commandline
*
* @param args Command line arguments.
*/
public static void main(String[] args) throws IOException
{
AddImageToPDF app = new AddImageToPDF();
if( args.length != 3 )
{
app.usage();
}
else
{
app.createPDFFromImage( args[0], args[1], args[2] );
}
}
/**
* This will print out a message telling how to use this example.
*/
private void usage()
{
System.err.println( "usage: " + this.getClass().getName() + " <input-pdf> <image> <output-pdf>" );
}
}
這篇關于Java:使用PDFBox 1庫從圖像創建PDF頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,