Add a picture chapter to pdf

package com.zkingcai.scf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class test {
	public static void main(String[] args) throws Exception {
	// template file path
        String templatePath = "f:/template.pdf";
        // Generated file path
        String targetPath = "f:/result.pdf";
        // image path
        String imagePath = "f:/zkcSeal.png";
        // read template file
        FileInputStream input = new FileInputStream(new File(templatePath));        
        PdfReader reader = new PdfReader(input);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(targetPath));
        Document document = new Document();
        // Get the page and coordinates from the domain name, the lower left corner is the starting point
        float x = document.getPageSize().getWidth() - 440;
        float y = document.getPageSize().getHeight() - 480;
        // read image
        Image image = Image.getInstance(imagePath);
        // Get the page for the operation
        PdfContentByte under = stamper.getOverContent (6);
        // scale the image according to the size of the field
        image.scaleToFit(document.getPageSize().getWidth() - 350, document.getPageSize().getHeight() - 290);
        // add pictures
        image.setAbsolutePosition(x, y);
        under.addImage (image);

        stamper.close();
        reader.close();
	}
}

image to add


Original pdf



added pdf


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325857870&siteId=291194637