iText-如何在现有PDF上标记图像并创建锚定

iText-如何在现有PDF上标记图像并创建锚定,itext,Itext,我有一个现有的文件,我想在上面的绝对位置印上一个图像。 我可以做到这一点,但我也想让这个图像可点击:当用户点击 在图片上,我希望PDF转到文档的最后一页 这是我的密码: PdfReader readerOriginalDoc = new PdfReader("src/main/resources/test.pdf"); PdfStamper stamper = new PdfStamper(readerOriginalDoc,new FileOutputStream("NewS

我有一个现有的文件,我想在上面的绝对位置印上一个图像。 我可以做到这一点,但我也想让这个图像可点击:当用户点击 在图片上,我希望PDF转到文档的最后一页

这是我的密码:

PdfReader readerOriginalDoc = new PdfReader("src/main/resources/test.pdf");         
PdfStamper stamper = new PdfStamper(readerOriginalDoc,new FileOutputStream("NewStamper.pdf"));
PdfContentByte content = stamper.getOverContent(1);
Image image = Image.getInstance("src/main/resources/images.jpg");
image.scaleAbsolute(50, 20);
image.setAbsolutePosition(100, 100);
image.setAnnotation(new Annotation(0, 0, 0, 0, 3));
content.addImage(image);
stamper.close();

你知道怎么做吗?

你使用的技术只有在从头开始创建文档时才有效

请查看示例,了解如何添加图像和链接,使该图像可单击到现有文档:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    Image img = Image.getInstance(IMG);
    float x = 10;
    float y = 650;
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    img.setAbsolutePosition(x, y);
    stamper.getOverContent(1).addImage(img);
    Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);
    PdfDestination destination = new PdfDestination(PdfDestination.FIT);
    PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
            linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
            reader.getNumberOfPages(), destination);
    link.setBorder(new PdfBorderArray(0, 0, 0));
    stamper.addAnnotation(link, 1);
    stamper.close();
}
您已经有了关于正确添加图像的部分。请注意,我为图像的位置及其尺寸创建了参数:

float x = 10;
float y = 650;
float w = img.getScaledWidth();
float h = img.getScaledHeight();
我使用这些值创建一个
矩形
对象:

Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);
这是我们使用
pdfanRotation
类创建的链接注释的位置。您需要使用
addAnnotation()
方法单独添加此批注

您可以在此处查看结果:
如果您单击i图标,您将跳转到文档的最后一页。

那么是否有任何方法可以反转此代码?我的意思是,我已经添加到pdf文件的链接注释图像,现在我需要删除它。removeAnnotation()只需删除注释,但图标仍然存在?有什么想法吗??)