我将 315p x 315p 图像添加到 pdf 文件作为图章。但它变大了:
如您所见,它现在的宽度约为 420p。pdf 文件未缩放。
我紧紧跟着导游。
我以为是Rectangle2D
,所以我根据这个指南创建了一个矩形,它的宽度和高度是正确的:
这里有什么问题?
这是我的源代码:
pom.xml 的一部分
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
主要方法:
public static void main(String[] args) {
String inputPdfPath = "F:/TEMP/a.pdf";
String outputPdfPath = "F:/TEMP/a2.pdf";
String stampImagePath = "C:/Users/Administrator/nextcloud/company/lanzhong/210528审批优化/approved.png";
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF document
doc.loadFromFile(inputPdfPath);
//Get the last page
PdfPageBase page = doc.getPages().get(doc.getPages().getCount() - 1);
//Load an image file
PdfImage image = PdfImage.fromFile(stampImagePath);
//Get the width and height of the image
int width = image.getWidth();
int height = image.getHeight();
//Create a PdfTemplate object based on the size of the image
PdfTemplate template = new PdfTemplate(width, height);
//Draw image on the template
template.getGraphics().drawImage(image, 0, 0, width, height);
//Create a rubber stamp annotation, specifying its location and position
Rectangle2D rect = new Rectangle2D.Float((float)(10), (float)(10), width, height);
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);
//Create a PdfAppearance object
PdfAppearance pdfAppearance = new PdfAppearance(stamp);
//Set the template as the normal state of the appearance
pdfAppearance.setNormal(template);
//Apply the appearance to the stamp
stamp.setAppearance(pdfAppearance);
//Add the stamp annotation to PDF
page.getAnnotationsWidget().add(stamp);
//Save the file
doc.saveToFile(outputPdfPath);
doc.close();
}