我有我愿把我的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的如何处理不同的图像扩展。
你可以使用另一种方法:调整图像大小“手动”(即通过图像处理软件),而不是通过编程的iText。
由于最终的尺寸似乎硬编码的,你可以使用一个已经调整尺寸的图像和每一个你水印的PDF文档时保存自己一些处理时间。
我那样做:
//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);
也许老的风格,但它为我工作;)
使用
watermark_image.scaleAbsolute(826, 1100);
代替
watermark_image.scaleToFit(826, 1100);
万一如果图像高度超过原稿的高度:
float documentWidth = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);
您可以使用
imageInstance.scaleAbsolute(requiredWidth, requiredHeight);
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");
//以上程序运行非常好,与缩放图像,希望你会发现它过于完美,我们可以扩展任何图像格式。
有时,如果图像是太大,如果图像适合OK不能按比例增长是只缩小有用。 换句话说,如果宽度不适合可用宽度或高度不适合可用高度的比例才会发生。 这是可以做到如下:
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;
}