Java Tutorial/PDF/Template
add Template
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4);
PdfReader reader = new PdfReader("1.pdf");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("my.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
cb.addTemplate(page, -0.5f, 0f, 0f, -0.5f, PageSize.A4.width() / 2, PageSize.A4.height());
document.close();
}
}
Create a template
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
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("2.pdf"));
document.open();
Image img = Image.getInstance("dog.jpg");
float w = img.scaledWidth();
float h = img.scaledHeight();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate t1 = cb.createTemplate(w / 2, h / 2);
t1.addImage(img, w, 0, 0, h, 0, -h / 2);
cb.addTemplate(t1, 36, PageSize.A4.height() - 36 - h / 2);
document.close();
}
}