iText图像调整大小

Fai*_*afe 19 java pdf watermark image itext

我有一个水印,我想把它放到我的PDF格式.水印是.bmp图像,并且是2290 x 3026.我在尝试调整此图片以适应页面时遇到了很多麻烦,有没有人有任何建议?

Document document = new Document(); 
PdfWriter.getInstance(document, new FileOutputStream("result.pdf")); 
document.open(); 
document.add(new Paragraph("hello")); 
document.close(); 
PdfReader reader = new PdfReader("result.pdf"); 
int number_of_pages = reader.getNumberOfPages(); 
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream("result_watermark.pdf")); 
// Get the PdfContentByte type by pdfStamper. 
Image watermark_image = Image.getInstance("abstract(0307).bmp"); 
int i = 0; 
watermark_image.setAbsolutePosition(0, 0);
watermark_image.scaleToFit(826, 1100);
System.out.println(watermark_image.getScaledWidth());
System.out.println(watermark_image.getScaledHeight()); 
PdfContentByte add_watermark; 
while (i < number_of_pages) { 
    i++; 
    add_watermark = pdfStamper.getUnderContent(i); 
    add_watermark.addImage(watermark_image); 
} 
pdfStamper.close();
Run Code Online (Sandbox Code Playgroud)

这是getScaled()方法的输出.

826.0 - Width
1091.4742 - Height
Run Code Online (Sandbox Code Playgroud)

我会和你们分享pdf的图片,但不幸的是我不能.

我应该尝试使用.jpg吗?我真的不知道iText如何处理不同的图像扩展.

Fra*_*ner 50

我这样做:

//if you would have a chapter indentation
int indentation = 0;
//whatever
Image image = coolPic;

float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
               - document.rightMargin() - indentation) / image.getWidth()) * 100;

image.scalePercent(scaler);
Run Code Online (Sandbox Code Playgroud)

也许是旧式但它对我有用;)

  • 这种方法比 Alexis 提出的想法对我更有效,因为默认缩放的分辨率非常低,因此如果您调整原始图像的大小以适合 pdf 文档,您会得到一个像素化的低分辨率图像,可能相当于 72 dpi。但是通过缩放,您可以使用更大的图像调整大小以适应但保留高分辨率。 (3认同)

MGD*_*oid 17

使用

watermark_image.scaleAbsolute(826, 1100);
Run Code Online (Sandbox Code Playgroud)

代替

watermark_image.scaleToFit(826, 1100);
Run Code Online (Sandbox Code Playgroud)


小智 15

以防万一图像高度超过文档高度:

float documentWidth = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);
Run Code Online (Sandbox Code Playgroud)

  • .height() 和 .width() 似乎不是有效的方法名称。我不得不更改为 document.getPageSize().getWidth() 和 document.getPageSize().getHeight() 以使其工作。 (2认同)

小智 14

您可以使用

imageInstance.scaleAbsolute(requiredWidth, requiredHeight);
Run Code Online (Sandbox Code Playgroud)


Ale*_*eon 8

您可以使用另一种方法:"手动"(即通过图像处理软件)调整图像大小,而不是通过iText以编程方式.

由于最终尺寸似乎是硬编码的,因此您可以使用已调整大小的图像,并在每次为PDF文档添加水印时节省一些处理时间.


小智 5

Document document = new Document();
PdfWriter.getInstance(document, new 
FileOutputStream("F:\\Directory1\\sample1.pdf"));
document.open();

Image img = 
Image.getInstance("F:\\Server\\Xyz\\WebContent\\ImageRestoring\\Rohit.jpg");
img.scaleToFit(200, 200);
document.add(new Paragraph("Sample 1: This is simple image demo."));
document.add(img);
document.close();
System.out.println("Done");
Run Code Online (Sandbox Code Playgroud)