PDFBox - 从图片生成PDF的问题

7
我会尽力为您翻译以下技术相关内容。该段文本涉及生成来自JPEG、BMP类型图像的PDF,并且总是在右侧截断一部分图像。我正在使用Windows默认图片Sunset.jpg。
下面是代码:
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.imageio.stream.FileImageInputStream;
    import org.apache.pdfbox.exceptions.COSVisitorException;
    import org.apache.pdfbox.io.RandomAccessFile;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.PDPage;
    import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt;
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;

    public class ImageToPDF 
    {
        public void createPDFFromImage( String file, String image) throws IOException, COSVisitorException
        {
            PDDocument doc = null;
            try
            {
                doc = new PDDocument();
                PDPage page = new PDPage();
                doc.addPage( page );
                PDXObjectImage ximage = null;
                if( image.toLowerCase().endsWith( ".jpg" ) || image.toLowerCase().endsWith( ".jpeg" ))
                {        
                    BufferedImage awtImage = ImageIO.read( new File( image ) );             
            ximage = new PDJpeg(doc, awtImage, 0 );
                }
                else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
                {
                     ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
                }
                else
                {
                         BufferedImage awtImage = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_RGB);             
                     awtImage = ImageIO.read(new FileImageInputStream(new File( image )));                              
                     ximage = new PDPixelMap(doc, awtImage);
                }
                System.out.println(" Width of the image.... "+ximage.getWidth());
                PDPageContentStream contentStream = new PDPageContentStream(doc, page);            
                contentStream.drawImage( ximage, 20, 20 );
                contentStream.close();
                doc.save( file );
           }
           finally
           {
                if( doc != null )
                {
                    doc.close();
                }
           }
       }

       public static void main(String[] args)
       {
            ImageToPDF app = new ImageToPDF();
            try
            {
                 app.createPDFFromImage( "C:\\test1.pdf", "C:\\Sunset.jpg");                        
            }
            catch (Exception e)
            {
                 e.printStackTrace();
            }
       }

请帮助我纠正我的错误。

在IT技术方面,我需要您的帮助。

}


你在这里找到了问题的答案吗?如果是的话,请分享一下,因为我也遇到了同样的问题。 - Anuj
你尝试过最新的版本了吗?你是否正确地包含了所有库,如Jemp和Tempbox? - MemLeak
5个回答

10

这些代码可能对你有帮助,它能正常工作。

    public void createPDFFromImage(String pdfFile, 
        List<String> imgList,int x, int y, float scale) throws IOException, COSVisitorException {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();
        Iterator iter = imgList.iterator();
        int imgIndex=0;
        while(iter.hasNext()) {
            PDPage page = new PDPage();
            doc.addPage(page);

            BufferedImage tmp_image = ImageIO.read(new File(iter.next().toString()));
            BufferedImage image = new BufferedImage(tmp_image.getWidth(), tmp_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);        
            image.createGraphics().drawRenderedImage(tmp_image, null);

            PDXObjectImage ximage = new PDPixelMap(doc, image);

            imgIndex++;


            PDPageContentStream contentStream = new PDPageContentStream(
                    doc, page,true,true);

            contentStream.drawXObject(ximage, x, y, ximage.getWidth()*scale, ximage.getHeight()*scale);

            contentStream.close();
        }
        doc.save(pdfFile);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

1
在这段代码中加入了“contentStream.drawXObject”行,因此得分为+10。 - Tilman Hausherr
1
v2.0.0:drawXObject - "@deprecated 使用drawImage或drawForm代替。" - Jeffrey Knight

0
请添加以下代码片段并尝试:

所需导入:

import org.apache.pdfbox.pdmodel.common.PDRectangle;

代码添加:

PDXObjectImage image = new PDJpeg(pdfDoc, filePath));

float w = image.getWidth();
float h = image.getHeight();

PDPage page = new PDPage(new PDRectangle (w,h));

0
你有没有考虑裁剪图片以防止被切掉?
contentStream.drawImage( ximage, 20, 20, croppedWidth, croppedHeight );

0
请尝试将drawImage语句修改为以下内容:
content.drawImage(ximage,0 /*or your preferred indent*/,(700-ximage.getHeight()));

0

这里有一个可用的程序例程(使用PDFBox 2.0.x版本),可以将一个BufferedImage(例如签名)添加到给定的PDF文档字节数组中。结果再次是一个字节数组。

    public static byte[] addBufferedImageIntoPdfPage(byte[] pdfDocumentData, BufferedImage imageToAdd, int pageNumber, int posX, int posY, float scaleImage) throws IOException
{
    PDDocument originalDocument = PDDocument.load(new ByteArrayInputStream(pdfDocumentData));
    PDImageXObject pdImage = LosslessFactory.createFromImage(originalDocument, imageToAdd);
    PDPage page = originalDocument.getPage(pageNumber);
    PDPageContentStream contentStream = new PDPageContentStream(originalDocument, page, PDPageContentStream.AppendMode.APPEND, true, true);

    // better method inspired by https://dev59.com/zmvXa4cB1Zd3GeqPL7ec#22318681
    contentStream.drawImage(pdImage, posX, posY, pdImage.getWidth() * scaleImage, pdImage.getHeight() * scaleImage);
    contentStream.close();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    originalDocument.save(outputStream);
    return outputStream.toByteArray();
}

希望这能有所帮助。干杯!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接