找回密码
 立即注册
thedocumentPDFBoxlicense | 软件设计/软件工程 2022-05-03 235 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
问题
我需要将图像(主要是 JPEG)直接转换为 PDF 文档的 PDF 页面。

可能是图像大小不同。

每个 PDF 页面都应具有与图像相同的精确尺寸。

所以每个页面只包含全分辨率图像。

我怎样才能做到这一点,即页面设置为图像/内容的尺寸?

因为我看到 PDF 文件具有不同的页面大小和方向,但是如何使用 PDFBox 实现这一点?

回答
我已经用以下代码解决了这个问题:
  1. PDDocument document = new PDDocument();
  2. InputStream in = new FileInputStream(someImage);
  3. BufferedImage bimg = ImageIO.read(in);
  4. float width = bimg.getWidth();
  5. float height = bimg.getHeight();
  6. PDPage page = new PDPage(new PDRectangle(width, height));
  7. document.addPage(page);
  8. PDXObjectImage img = new PDJpeg(document, new FileInputStream(someImage));
  9. PDPageContentStream contentStream = new PDPageContentStream(document, page);
  10. contentStream.drawImage(img, 0, 0);
  11. contentStream.close();
  12. in.close();

  13. document.save("test.pdf");
  14. document.close();
复制代码

为了完整起见,PDFBox 2+ API 的当前 PDFBox 示例中的代码
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements.  See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License.  You may obtain a copy of the License at
  8. *
  9. *      http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.pdfbox.examples.pdmodel;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import org.apache.pdfbox.pdmodel.PDDocument;
  21. import org.apache.pdfbox.pdmodel.PDPage;
  22. import org.apache.pdfbox.pdmodel.PDPageContentStream;
  23. import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
  24. import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

  25. /**
  26. * This is an example that creates a reads a document and adds an image to it..
  27. *
  28. * The example is taken from the pdf file format specification.
  29. *
  30. * @author Ben Litchfield
  31. */
  32. public class AddImageToPDF
  33. {
  34.     /**
  35.      * Add an image to an existing PDF document.
  36.      *
  37.      * @param inputFile The input PDF to add the image to.
  38.      * @param imagePath The filename of the image to put in the PDF.
  39.      * @param outputFile The file to write to the pdf to.
  40.      *
  41.      * @throws IOException If there is an error writing the data.
  42.      */
  43.     public void createPDFFromImage( String inputFile, String imagePath, String outputFile )
  44.             throws IOException
  45.     {
  46.         try (PDDocument doc = PDDocument.load(new File(inputFile)))
  47.         {
  48.             //we will add the image to the first page.
  49.             PDPage page = doc.getPage(0);

  50.             // createFromFile is the easiest way with an image file
  51.             // if you already have the image in a BufferedImage,
  52.             // call LosslessFactory.createFromImage() instead
  53.             PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);

  54.             try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true))
  55.             {
  56.                 // contentStream.drawImage(ximage, 20, 20 );
  57.                 // better method inspired by http://stackoverflow.com/a/22318681/535646
  58.                 // reduce this value if the image is too large
  59.                 float scale = 1f;
  60.                 contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth() * scale, pdImage.getHeight() * scale);
  61.             }
  62.             doc.save(outputFile);
  63.         }
  64.     }

  65.     /**
  66.      * This will load a PDF document and add a single image on it.
  67.      * <br>
  68.      * see usage() for commandline
  69.      *
  70.      * @param args Command line arguments.
  71.      */
  72.     public static void main(String[] args) throws IOException
  73.     {
  74.         AddImageToPDF app = new AddImageToPDF();
  75.         if( args.length != 3 )
  76.         {
  77.             app.usage();
  78.         }
  79.         else
  80.         {
  81.             app.createPDFFromImage( args[0], args[1], args[2] );
  82.         }
  83.     }

  84.     /**
  85.      * This will print out a message telling how to use this example.
  86.      */
  87.     private void usage()
  88.     {
  89.         System.err.println( "usage: " + this.getClass().getName() + " <input-pdf> <image> <output-pdf>" );
  90.     }
  91. }
复制代码






上一篇:opencv imwrite,图像旋转
下一篇:警告:JSF1091:找不到文件动态内容的 mime 类型