【问题标题】:converting image to pdf file using itext library使用 itext 库将图像转换为 pdf 文件
【发布时间】:2014-06-26 20:42:34
【问题描述】:

我能够使用 itext 库成功地将图像转换为 pdf,但问题是在转换时。它没有正确转换(即)确切图像的位置像这样向下拖动。

这是pdf图片....

在将我的图像转换回 pdf 后,这就是我得到的

这是我正在使用的代码

用于从 pdf 转换为图像

File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
String fileName = sourceFile.getName().replace(".pdf", "");
if (sourceFile.exists()) {
    if (!destinationFile.exists()) {
        destinationFile.mkdir();
        System.out.println("Folder created in: "+ destinationFile.getCanonicalPath());
    }

    RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdf = new PDFFile(buf);
    System.out.println("Total Pages: "+ pdf.getNumPages());
    int pageNumber = 1;
    for (int i = 0; i < pdf.getNumPages(); i++) {
        PDFPage page = pdf.getPage(i+1);
        // image dimensions 
        int width = 1200;
        int height = 1400;
        // create the image
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done
        Image image = page.getImage(width, height, rect, null, true, true );
        Graphics2D bufImageGraphics = bufferedImage.createGraphics();
        bufImageGraphics.drawImage(image, 0, 0, null);

        File imageFile = new File( destinationDir + "pdfImg" + i +".png" );// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp

        ImageIO.write(bufferedImage, "png", imageFile);
        pageNumber++;                
    }
}

这是我用来再次将其转换回 pdf 的代码

Rectangle pageSize = new Rectangle(2780, 2525);
Document pdfDocument = new Document(PageSize.A4);
String pdfFilePath = outputFile;
try
{
    FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);
    PdfWriter writer = null;
    writer = PdfWriter.getInstance(pdfDocument, fileOutputStream);
    writer.open();
    pdfDocument.open();
    /**
    * Proceed if the file given is a picture file
    */
    if (isPictureFile)
    {
        pdfDocument.add(com.itextpdf.text.Image.getInstance(inputFile));
    }
    /**
    * Proceed if the file given is (.txt,.html,.doc etc)
    */
    else
    {
    File file = new File(inputFile);
    //pdfDocument.add(new Paragraph(org.apache.commons.io.FileUtils.readFileToString(file)));
    }

    pdfDocument.close();
    writer.close();
}
catch (Exception exception)
{
    System.out.println("Document Exception!" + exception);
}

【问题讨论】:

    标签: java pdf itext


    【解决方案1】:

    在此处更改图像大小:

    // image dimensions 
                int width = 700; // here
                int height = 600;//here
    

    您的图片超出了 pdf screenSize...将其缩小到 pdf 中可以接受的大小..

    【讨论】:

    • thanx allot dude 这样一个简单的答案,它对我有用thanx allot dude :)
    • 如果您从这个答案中得到了解决方案,请接受这个答案以将其保留在已解决的问题中..
    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2014-06-13
    相关资源
    最近更新 更多