MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
    public static void main(String[] args) throws Exception {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("clipping_path.pdf"));
        document.open();
        Image img = Image.getInstance("dog.jpg");
        float w = img.scaledWidth();
        float h = img.scaledHeight();
        PdfContentByte cb = writer.getDirectContent();

        PdfTemplate tp1 = cb.createTemplate(w, h);
        img.setAbsolutePosition(0, 0);
        tp1.roundRectangle(0, 0, w, h, 10);
        tp1.clip();
        tp1.newPath();
        tp1.addImage(img);
        cb.addTemplate(tp1, 36, 420);

        document.close();
    }
}