23

我有一个水印,我想将其放入我的 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 处理不同图像扩展的能力如何。

4

7 回答 7

63

我这样做:

//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);
于 2012-06-20T14:19:44.960 回答
22

采用

watermark_image.scaleAbsolute(826, 1100);

代替

watermark_image.scaleToFit(826, 1100);
于 2013-01-11T11:40:30.233 回答
16

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

float documentWidth = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);
于 2014-10-14T12:01:43.790 回答
15

您可以使用

imageInstance.scaleAbsolute(requiredWidth, requiredHeight);
于 2013-04-05T05:24:57.103 回答
7

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

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

于 2012-06-25T19:15:10.503 回答
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");
于 2018-01-23T11:12:04.100 回答
3

有时,仅当图像太大时才按比例缩小,如果图像合适,则不要按比例放大。换句话说,只有当宽度不适合可用宽度或高度不适合可用高度时,才会发生缩放。这可以按如下方式完成:

float scaleRatio = calculateScaleRatio(doc, image);
if (scaleRatio < 1F) {
    image.scalePercent(scaleRatio * 100F);
}

用这个calculateScaleRatio方法:

/**
 * Calculate scale ratio required to fit supplied image in the supplied PDF document.
 * @param doc    PDF to fit image in.
 * @param image  Image to be converted into a PDF.
 * @return       Scale ratio (0.0 - 1.0), or 1.0 if no scaling is required.
 */
private float calculateScaleRatio(Document doc, Image image) {
    float scaleRatio;
    float imageWidth = image.getWidth();
    float imageHeight = image.getHeight();
    if (imageWidth > 0 && imageHeight > 0) {
        // Firstly get the scale ratio required to fit the image width
        Rectangle pageSize = doc.getPageSize();
        float pageWidth = pageSize.getWidth() - doc.leftMargin() - doc.rightMargin();
        scaleRatio = pageWidth / imageWidth;

        // Get scale ratio required to fit image height - if smaller, use this instead
        float pageHeight = pageSize.getHeight() - doc.topMargin() - doc.bottomMargin();
        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;
}
于 2019-01-09T08:33:00.750 回答