gpt4 book ai didi

image - PDFBox图片大小问题

转载 作者:行者123 更新时间:2023-12-04 17:10:25 24 4
gpt4 key购买 nike

我是使用PdfBox的新手,在显示图像时遇到了一个小问题。我能够导入大小为800 * 900像素的图像,当在现有pdf中以100%的比例查看时看起来不错。但是,使用以下代码生成结果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();
}

最佳答案

如果您的意图是在PDF上使用A4大小的图片,那么我想您会发现典型A4的实际大小(以像素为单位)。

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

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

contentStream.drawImage(img, 10, 700);

图片的扩展名是:jpg
InputStream in = new FileInputStream(new File("img.jpg"));

检查那些并返回更多信息。

就这样。
祝你好运'''

关于image - PDFBox图片大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com