我有一个水印,我想加入我的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();
以下是getScaled()
方法的输出。
826.0 - Width
1091.4742 - Height
我会和你们分享pdf的图片但不幸的是我不能。
我应该尝试使用.jpg吗?我真的不知道iText如何处理不同的图像扩展。
答案 0 :(得分:47)
我这样做:
//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);
也许是旧式但它对我有用;)
答案 1 :(得分:17)
使用
watermark_image.scaleAbsolute(826, 1100);
而不是
watermark_image.scaleToFit(826, 1100);
答案 2 :(得分:14)
以防图像高度超过文档高度:
float documentWidth = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);
答案 3 :(得分:12)
你可以使用
imageInstance.scaleAbsolute(requiredWidth, requiredHeight);
答案 4 :(得分:7)
您可以使用其他方法:“手动”(即通过图像处理软件)调整图像大小,而不是通过iText以编程方式。
由于最终尺寸似乎是硬编码的,因此您可以使用已调整大小的图像,并在每次为PDF文档添加水印时节省一些处理时间。
答案 5 :(得分:3)
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");
//以上程序运行完美,缩放图像,希望您也能找到完美的图像格式。我们可以扩展任何图像格式。
答案 6 :(得分:0)
有时,如果图像太大,仅缩小 很有用,如果图像合适,则不放大。换句话说,仅当宽度不适合可用宽度或高度不适合可用高度时,才进行缩放。可以按照以下步骤进行操作:
float scaleRatio = calculateScaleRatio(iTextDocument, image);
if (scaleRatio < 1F) {
image.scalePercent(scaleRatio * 100F);
}
使用此calculateScaleRatio
方法:
/**
* Calculates the scale ratio required to fit the supplied image in the supplied PDF document
*
* @param iTextDocument
* PDF to fit image in.
* @param image
* Image to be converted into a PDF.
* @return The required scale percentage or 100% if no scaling is required to fit the image.
*/
private float calculateScaleRatio(Document iTextDocument, Image image) {
float scaleRatio;
final float imageWidth = image.getWidth();
final float imageHeight = image.getHeight();
if (imageWidth > 0 && imageHeight > 0) {
final Rectangle pageSize = iTextDocument.getPageSize();
// First get the scale ratio required to fit the image width
final float pageWidth = pageSize.getWidth() - iTextDocument.leftMargin() - iTextDocument.rightMargin();
scaleRatio = pageWidth / imageWidth;
// Now get the scale ratio required to fit the image height - and if smaller, use this instead
final float pageHeight = pageSize.getHeight() - iTextDocument.topMargin() - iTextDocument.bottomMargin();
final float heightScaleRatio = pageHeight / imageHeight;
if (heightScaleRatio < scaleRatio) {
scaleRatio = heightScaleRatio;
}
// Do not upscale - if the entire image can fit in the page, leave it unscaled.
if (scaleRatio > 1F) {
scaleRatio = 1F;
}
} else {
// No scaling if the width or height is zero.
scaleRatio = 1F;
}
return scaleRatio;
}