本文介绍了PDFBox-从图像生成PDF的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JPEG,BMP类型的图像生成PDF,但我总是右部分图像被截断.我正在使用默认的Windows图片之一Sunset.jpg.

I am trying to generate a PDF from images of type JPEG, BMP but i am gettng part of the image on the right always getting cut off. I am using one of the default windows picture 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();
            }
       }

}

请帮助我纠正我做错的事情.

Please help me in correcting what i am doing wrong.

推荐答案

这些代码可能对您有帮助,可以使用.

These code may helpful to you,It works.

    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();
        }
    }
}

这篇关于PDFBox-从图像生成PDF的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:08