有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用iText在pdf中调整图像大小。布局

我想在生成的pdf文件中,除了标题之外,再添加一个应用程序的徽标。我使用下面的代码这样做,但图像没有缩小。感谢您的帮助

// Creating Paragraphs
String heading = "Declaration Letter";
Paragraph head = new Paragraph(heading).setBold().setFontSize(20);

Paragraph paragraph1 = new Paragraph();
paragraph1.add("This is to certify that the Product Serial Number ");

// Adding paragraphs to document
ImageData data = ImageDataFactory.create("img.png");
Image image = new Image(data);
// set Absolute Position
//image.setFixedPosition(450f, 10f);
image.scaleAbsolute(20f, 20f);
// set Scaling
image.setAutoScaleHeight(true);
image.setAutoScaleWidth(true);
    
document.add(image);
document.add(head);
document.add(space);
document.add(space);
document.add(paragraph1);

现在看起来像是图1。我想要标题左边的小图标

image1


共 (1) 个答案

  1. # 1 楼答案

    首先,确保您使用的是com.itextpdf.text.Image类。 我认为您没有使用itext

    对于itextImage类,这将是正确的方法:

    Image img = Image.getInstance("Path to your image");
    img.scaleAbsolute(150,50);