【问题标题】:PDFBox image size issuesPDFBox 图像大小问题
【发布时间】:2013-07-27 06:31:37
【问题描述】:

我是使用 PdfBox 的新手,在显示图像时遇到了一个小问题。我能够导入尺寸为 800*900 像素的图像,并且在以 100% 的现有 pdf 格式查看时看起来不错。但是,当使用以下代码生成生成的 PDF 时,图像会变得模糊,并且图像会超出 A4 页面的边界。

是否有不同的方式来调整/保存图像以便它们在 pdfbox 中正确显示?

public class PDFtest {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException, COSVisitorException {
    // TODO code application logic here
    // Create a document and add a page to it
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
    document.addPage(page);

    // Create a new font object selecting one of the PDF base fonts
    PDFont font = PDType1Font.HELVETICA_BOLD;
    InputStream in = new FileInputStream(new File("img.jpg"));
    PDJpeg img = new PDJpeg(document, in);
    // Start a new content stream which will "hold" the to be created content
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"

    contentStream.drawImage(img, 10, 700);
    contentStream.beginText();        
    contentStream.setFont(font, 12);
    contentStream.moveTextPositionByAmount(10, 650);
    contentStream.drawString("Hello World");
    contentStream.endText();

    // Make sure that the content stream is closed:
    contentStream.close();

    // Save the results and ensure that the document is properly closed:
    document.save("Hello World.pdf");
    document.close();
    }

【问题讨论】:

    标签: image pdfbox


    【解决方案1】:

    我想指出,从 2.0 开始,Victor 的答案中的 contentStream.drawXObject 函数调用已被弃用。如果你想指定宽度和高度,应该使用contentStream.drawImage(image, x, y, width, height)

    【讨论】:

      【解决方案2】:

      我在这个问题中遇到了同样的问题,但给出的答案不正确。

      经过一番研究,我找到了解决方案。

      不要使用函数drawImage,而是使用函数drawXObject

      contentStream.drawXObject( img, 10, 700, 100, 100 );
      

      最后两个数字指定要绘制的图像的大小。

      【讨论】:

      • drawXObject 在 2.0 中被弃用。
      • 使用drawImagedrawXObject的实例
      【解决方案3】:

      对于类似的情况,对我来说,使用 PDF 2.0.11 和尺寸为 1600 x 2100 的 tiff 文件,以下代码完全适合 A4(纵向)尺寸的图像。不确定 PDFRectangle 是否适合您。

      我直接从 PDFBOX 得到这个例子 - Example

      我唯一调整/介绍的是:

      PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight()
      

      这是完整的示例:

      public static void main(String[] args) throws IOException
      {
      //        if (args.length != 2)
      //        {
      //            System.err.println("usage: " + ImageToPDF.class.getName() + " <image> <output-file>");
      //            System.exit(1);
      //        }
      
          String imagePath = "C:/FAX/sample.tiff";
          String pdfPath = "C:/FAX/sample.pdf";
      
          if (!pdfPath.endsWith(".pdf"))
          {
              System.err.println("Last argument must be the destination .pdf file");
              System.exit(1);
          }
      
          try (PDDocument doc = new PDDocument())
          {
              PDPage page = new PDPage();
              doc.addPage(page);
      
              // 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);
      
              // draw the image at full size at (x=20, y=20)
              try (PDPageContentStream contents = new PDPageContentStream(doc, page))
              {
                  // draw the image at full size at (x=20, y=20)
                  contents.drawImage(pdImage, 0, 0, PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight());
      
                  // to draw the image at half size at (x=20, y=20) use
                  // contents.drawImage(pdImage, 20, 20, pdImage.getWidth() / 2, pdImage.getHeight() / 2); 
              }
              doc.save(pdfPath);
              System.out.println("Tiff converted to PDF succussfully..!");
          }
      }
      

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        如果您的意图是 PDF 上的 A4 大小的图片,那么我猜您会找到典型 A4 的实际大小(以像素为单位)。

        此外,您还应该注意要查看的图片的扩展名,例如 jpg、gif 或 bmp ...

        根据我在您的代码中看到的,图片的尺寸是 10 X 700,我认为这是非常小的尺寸。

        contentStream.drawImage(img, 10, 700);
        

        而且图片的扩展名是:jpg

        InputStream in = new FileInputStream(new File("img.jpg"));
        

        检查这些并返回以获取更多信息。

        就是这样。 祝你好运'''

        【讨论】:

        • 你对第一部分很满意,我认为它会那么简单。在您发布前 2 秒,我意识到最大宽度为 595 像素,每英寸像素密度相对较低。关于 (img,10,700),图像尺寸取自实际的 jpeg,然后这两个数字代表绘制图像的位置和 x,y 坐标,而不是图像尺寸。
        • 啊哈,我明白了,无论如何你已经发现了你的错误,记住你从那个错误中吸取教训。
        • @user2624929 这里 contentStream.drawImage(img, 10, 700); 10X700 不是图像大小,它是图像应该出现的位置,表示 X,Y 坐标。你能告诉我如何使我的图像尺寸为 100X100 并适合那个位置吗??
        【解决方案5】:

        根据新的 API 2.0.x,可以使用 PDRectangle 来获取 Pdf 页面的宽度和高度。可以使用 PDPageContentStream 根据 PDF 页面绘制图像。 供参考:

        try (PDPageContentStream contents = new PDPageContentStream(pdDocument, pdPage)) {
                        final PDRectangle mediaBox = pdPage.getMediaBox();
                        final PDImageXObject pdImage = PDImageXObject.createFromFile(image, pdDocument);
                        contents.drawImage(pdImage, 0, 0, mediaBox.getWidth(), mediaBox.getHeight());
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 2017-12-09
          • 1970-01-01
          相关资源
          最近更新 更多