使用Java将图像或文本图章添加到PDF

本文介绍如何使用Free Spire.PDF for Java在PDF文档中添加图像和文本图章,常用于合同、报告等,以表明文档已被审查或标记为'已读'、'合格'、'保密'等。
摘要由CSDN通过智能技术生成

PDF stamps are often used in contracts, reports and restricted materials, to prove that the documents have been reviewed and marked as "read", "qualified", or "confidential", etc. This article will show you how we can add image stamps and text stamps to PDF documents by using Free Spire.PDF for Java.

Add Image Stamp

import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTemplate;

import java.awt.geom.Rectangle2D;

public class ImageStamp {

    public static void main(String[] args) {

        //create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //load a PDF document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //get the last page
        PdfPageBase page = doc.getPages().get(doc.getPages().getCount()-1);

        //load an image file
        PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\100-percent-original-stamp.png");

        //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) (page.getActualSize().getWidth() - width - 50), (float) (page.getActualSize().getHeight() - height - 80), 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);

        //the appearance to the stamp
        stamp.setAppearance(pdfAppearance);

        //add the stamp annotation to PDF
        page.getAnnotationsWidget().add(stamp);

        //save the file
        doc.saveToFile("output/AddImageStamp.pdf");
        doc.close();
    }
}

output:

Add Text Stamp

import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;

public class TextStamp {

    public static void main(String[] args) {

        //create a PdfDocument object
        PdfDocument document = new PdfDocument();

        //load a PDF file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //get the last page
        PdfPageBase page = document.getPages().get(document.getPages().getCount()-1);

        //create a pdf template
        PdfTemplate template = new PdfTemplate(185, 50);

        //create two fonts
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", Font.ITALIC,16), true);
        PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", Font.ITALIC  ,10), true);


        //create a solid brush and a gradient brush
        PdfSolidBrush solidBrush = new PdfSolidBrush(new PdfRGBColor(Color.blue));
        Rectangle2D rect1 = new Rectangle2D.Float();
        rect1.setFrame(new Point2D.Float(0,0),template.getSize());
        PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1,new PdfRGBColor(Color.white),new PdfRGBColor(Color.orange),PdfLinearGradientMode.Horizontal);

        //create rounded rectangle path
        int CornerRadius = 20;
        PdfPath path = new PdfPath();
        path.addArc(template.getBounds().getX(), template.getBounds().getY(), CornerRadius, CornerRadius, 180, 90);
        path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius,template.getBounds().getY(), CornerRadius, CornerRadius, 270, 90);
        path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY()+ template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 0, 90);
        path.addArc(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.addLine( template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, template.getBounds().getX(), template.getBounds().getY() + CornerRadius / 2);

        //draw path on the template
        template.getGraphics().drawPath(linearGradientBrush, path);
        template.getGraphics().drawPath(PdfPens.getBlue(), path);

        //draw dynamic text on the template
        String s1 = "DENIED\n";
        String s2 = "By John " + dateToString(new java.util.Date(),"yyyy-MM-dd HH:mm:ss");
        template.getGraphics().drawString(s1, font1, solidBrush, new Point2D.Float(5, 5));
        template.getGraphics().drawString(s2, font2, solidBrush, new Point2D.Float(5, 28));

        //create a rubber stamp, specifying its size and location
        Rectangle2D rect2= new Rectangle2D.Float();
        rect2.setFrame(new Point2D.Float((float)(page.getActualSize().getWidth()-250),(float)(page.getActualSize().getHeight()-120)),  template.getSize());
        PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect2);

        //create a PdfAppearance object and apply the template as its normal state
        PdfAppearance appearance = new PdfAppearance(stamp);
        appearance.setNormal(template);

        //apply the appearance to stamp
        stamp.setAppearance(appearance);

        //add the stamp annotation to annotation collection
        page.getAnnotationsWidget().add(stamp);

        //save the file
        document.saveToFile("output/AddTextStamp.pdf");
        document.close();
    }

    //convert date to string
    public static String dateToString(java.util.Date poDate,String pcFormat) {
        SimpleDateFormat loFormat = new SimpleDateFormat(pcFormat);
        return loFormat.format(poDate);
    }
}

output:

from: https://dev.to//eiceblue/add-image-or-text-stamps-to-pdf-in-java-2p7g

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值