我可以使用任何开源库吗?如何使用java将PNG文件转换为PDF?
4
A
回答
6
itext可以帮助你。 你并没有真正将png转换为pdf,而是使用png创建了一个pdf。 简单的例子:
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter.getInstance(document, new FileOutputStream("C:/test.pdf"));
document.open();
Image image = Image.getInstance(getClass().getResource("/logo.png"));
document.add(image);
document.close();
0
阅读javax.imageio.ImageIO中 对PDF的iText:http://itextpdf.com
2
其旋转的页面,如果横向模式下,如果你是不是已经利用iText
更适合的/**
* Converts arbitrary image file to PDF
* http://stackoverflow.com/a/42937466/241986
* @param imageFile contents of JPEG or PNG file
* @param outputStream stream to write out pdf, always closed after this method execution.
* @throws IOException when there' an actual exception or image is not valid
*/
public static void imageToPdf(byte[] imageFile, OutputStream outputStream) throws IOException {
try {
Image image;
try {
image = Image.getInstance(imageFile);
} catch (BadElementException bee) {
throw new IOException(bee);
}
//See http://stackoverflow.com/questions/1373035/how-do-i-scale-one-rectangle-to-the-maximum-size-possible-within-another-rectang
Rectangle A4 = PageSize.A4;
float scalePortrait = Math.min(A4.getWidth()/image.getWidth(),
A4.getHeight()/image.getHeight());
float scaleLandscape = Math.min(A4.getHeight()/image.getWidth(),
A4.getWidth()/image.getHeight());
// We try to occupy as much space as possible
// Sportrait = (w*scalePortrait) * (h*scalePortrait)
// Slandscape = (w*scaleLandscape) * (h*scaleLandscape)
// therefore the bigger area is where we have bigger scale
boolean isLandscape = scaleLandscape > scalePortrait;
float w;
float h;
if (isLandscape) {
A4 = A4.rotate();
w = image.getWidth() * scaleLandscape;
h = image.getHeight() * scaleLandscape;
} else {
w = image.getWidth() * scalePortrait;
h = image.getHeight() * scalePortrait;
}
Document document = new Document(A4, 10, 10, 10, 10);
try {
PdfWriter.getInstance(document, outputStream);
} catch (DocumentException e) {
throw new IOException(e);
}
document.open();
try {
image.scaleAbsolute(w, h);
float posH = (A4.getHeight() - h)/2;
float posW = (A4.getWidth() - w)/2;
image.setAbsolutePosition(posW, posH);
image.setBorder(Image.NO_BORDER);
image.setBorderWidth(0);
try {
document.newPage();
document.add(image);
} catch (DocumentException de) {
throw new IOException(de);
}
} finally {
document.close();
}
} finally {
outputStream.close();
}
}
里面的pom.xml,免费iText的叉一个,一个例子
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.0.1</version>
</dependency>
相关问题
- 1. 如何使用Imagemagick将.PDF文件转换为.PNG PHP api
- 2. 使用蜻蜓将PDF转换为PNG
- 3. Java:使用apache POI如何将ms word文件转换为pdf?
- 4. 如何使用Java将HTML网页转换为PDF文件
- 5. 如何将.ai文件转换为PNG
- 6. 转换HTML文件为PDF使用Java
- 7. 如何将所有文件夹的PDF格式转换为PNG使用ghodtscript
- 8. 如何使用ghostscript将PDF转换为PNG?
- 9. 如何使用SVG图形将PDF转换为PDF文件?
- 10. 如何将PDF文件转换为java swing中的doc文件?
- 11. 需要将EMF转换为JPEG,PNG文件格式使用java
- 12. 如何使用Perl将PDF文件转换为DOC文件?
- 13. 如何使用ImageMagick将PDF文件转换为图像文件
- 14. 在Windows .bat文件中使用ghostscript将多个pdf文件转换为PNG
- 15. 如何将.CATDrawing文件转换为.pdf
- 16. 如何将gp4文件转换为pdf
- 17. 使用Magick.NET将PDF转换为PNG时缺少文本
- 18. Java ImageIO:我可以将PNG-24文件转换为PNG-8吗?
- 19. 如何将多个pdf文件转换为使用java的单个tiff文件?
- 20. 如何使用Ghostscript将psd(photoshop文件)或tiff转换为PNG?
- 21. 将jpg/png/txt或任何文件格式转换为pdf使用mvc5
- 22. 如何使用itext将文档转换为pdf文件
- 23. 将EPS/PDF转换为JPEG/PNG格式?
- 24. 将pdf转换为透明png
- 25. pdfbox2.0.4将PDF转换与中国为PNG
- 26. 将.png转换为“有损”.pdf
- 27. PDF转换为黑白PNG
- 28. 使用Java将EPUB转换为PDF
- 29. 使用Java将PDF转换为XML
- 30. 使用Java将PDF转换为Postscript