1
A
回答
2
你可能想看看pdfbox examples directory in the Apache SVN repository,特别是适当命名的示例类AddImageToPDF这个关键的方法:
public void createPDFFromImage(String inputFile, String imagePath, String outputFile)
throws IOException
{
// the document
PDDocument doc = null;
try
{
doc = PDDocument.load(new File(inputFile));
//we will add the image to the first page.
PDPage page = doc.getPage(0);
// 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);
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true);
// contentStream.drawImage(ximage, 20, 20);
// better method inspired by http://stackoverflow.com/a/22318681/535646
// reduce this value if the image is too large
float scale = 1f;
contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale);
contentStream.close();
doc.save(outputFile);
}
finally
{
if(doc != null)
{
doc.close();
}
}
}
相关问题
- 1. 替换PDImageXObject与另一个PDFBOX 2.0.3
- 2. 如何显示在图像
- 3. 如何在内容没有图像时显示图像并在显示图像时显示图像
- 4. 如何在pygame中显示图像?
- 5. 如何在Django中显示图像1.10
- 6. 如何在UNNotificationServiceExtension中显示图像?
- 7. 如何在AspxImage中显示图像?
- 8. 如何在Django中显示foreignkey图像
- 9. 如何在laravel中显示图像
- 10. 如何在框架中显示图像?
- 11. 如何在JTable中显示图像?
- 12. 如何在NSTableView中显示图像?
- 13. android:如何在textview中显示图像
- 14. 如何在ploco中显示plist图像
- 15. 如何在ImageView中显示图像?
- 16. 如何在ListView中显示图像android
- 17. 如何在行中显示图像?
- 18. 如何在框架中显示图像?
- 19. 如何在GridView中显示图像?
- 20. 如何在Facebook Sharer中显示图像?
- 21. 如何在grails GSP中显示图像?
- 22. 如何在jsp中显示图像?
- 23. 如何在android中显示图像组
- 24. 如何在Struts 2中显示图像?
- 25. 如何在PyQT GUI中显示图像?
- 26. 如何在Silverlight中显示图像?
- 27. 如何在Android中显示大图像
- 28. 如何在Angular中显示图像
- 29. 如何在MemoryStream中显示图像?
- 30. 如何在WP7中显示PNG图像?
您好,当我用局部图像路径是工作好吧,当我把图像在线路径(http://www.foodnavigator.com/var/plain_site/storage/images/publications/food-beverage-nutrition/foodnavigator.com/science-nutrition/high-cost-of-水果和蔬菜连接到高体脂肪中,年轻的孩子学习/ 8800391-3-ENG-GB /高性价比的水果和蔬菜连接到higher-体脂肪中,年轻的孩子-S tudy.jpg),那么它不会显示图像。请告诉我该怎么办。 – Manojkumar
@Manojkumar请分享结果PDF。如果您的代码与上面的代码不同,请创建一个新问题。 –
String imageUrls =“F:/me.png”; String imageUrls =“www.foodnavigator.com/var/plain_site/storage/images/publications/food-beverage-nutrition/foodnavigator.com/science-nutrition/high-cost-of-fruit-and-vegetables-linked-to - 更高的体脂肪,在年轻的子女学习/ 8800391-3-ENG-GB /高性价比的水果和蔬菜连接到高体脂肪中,年轻的孩子-Study.jpg“; PDImageXObject pdImage = PDImageXObject.createFromFile(imageUrls,document);contentStream.drawImage(pdImage,margin + 5,texty,170,100); – Manojkumar