【问题标题】:How can i convert images to pdf in java servlet?java - 如何在java servlet中将图像转换为pdf?
【发布时间】:2014-01-27 17:15:33
【问题描述】:

我在将图像转换为 pdf 文件时遇到了一些问题。

我完成了单张图片到 pdf 的转换,现在我想将多张图片转换成一个 pdf 文件。

我正在使用 itext 库来创建 pdf。

这是我创建只有一张图片的单个 pdf 的代码。

  Document convertJpgToPdf=new Document();
        //Create PdfWriter for Document to hold physical file
        PdfWriter.getInstance(convertJpgToPdf, new     FileOutputStream("D:\\PDF_Path\\ConvertImagetoPDF.pdf"));
        convertJpgToPdf.open();
        // Write Somethinf into that File
        convertJpgToPdf.add(new Paragraph("Welcome, Your Input Image is Converted to PDF, Save the File"));
        convertJpgToPdf.add(new Paragraph("PDF Produced by Converting Image to PDF as Servlet"));      
        //Get the input image to Convert to PDF
        Image convertJpg=Image.getInstance(uploadPath);
        //Add image to Document
        convertJpgToPdf.add(convertJpg);
        //Close Document
        convertJpgToPdf.close();

我的问题是我有一个文件夹,该文件夹包含 10 张图片,我如何将所有图片转换为单个 pdf 文件,

喜欢

       PDF File Header

       image1

       image2

       ..........
       ..........

       imageN

有没有可能。请帮我。提前致谢。

【问题讨论】:

标签: java servlets pdf itext


【解决方案1】:
Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    // Adding a series of images
    Image img;
    for (int i = 0; i < RESOURCES.length; i++) {
        img = Image.getInstance(String.format("resources/img/%s", RESOURCES[i]));
        if (img.getScaledWidth() > 300 || img.getScaledHeight() > 300) {
            img.scaleToFit(300, 300);
        }
        document.add(new Paragraph(
                String.format("%s is an image of type %s", RESOURCES[i], img.getClass().getName())));
        document.add(img);
    }

请阅读 itext 教程,它工作正常 click here

【讨论】:

  • +1 表示“阅读手册”,这是对该主题的唯一真正答案,但您非常友好地提供可复制/粘贴的代码。
  • 在此链接中直接提供图片,但我想打开文件夹以获取该图片,并且我正在使用 servlet
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 2017-07-09
  • 2016-12-08
  • 2020-07-07
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多