java - how to create watermark in PDF -
i have program generating money receipt organization. need add waterark in document sort of security. adding code below. pdf has been generating ok how add watermark?
/* * change template, choose tools | templates * , open template in editor. */ package com.example; import com.lowagie.text.*; import com.lowagie.text.pdf.*; import java.awt.color; import java.io.filenotfoundexception; import java.io.fileoutputstream; /** * * @author chandan */ public class moneryrecipt { // document m_pdfdocument; public static void main(string[] args) throws documentexception, filenotfoundexception { document m_pdfdocument = new document(pagesize.letter, 5, 5, 5, 5); try { docwriter m_docwriter; // pdfwriter m_pdfwriter = null; pdfwriter writer = pdfwriter.getinstance(m_pdfdocument, new fileoutputstream("e:/aa.pdf")); image logo=image.getinstance("f:/gmc_report.jpg"); m_pdfdocument.open(); image background = image.getinstance(logo); background.setabsoluteposition(200, 400); // phrase watermarkdd = new phrase("chandan sarma", new font(fontfactory.getfont(fontfactory.helvetica, 20,font.normal,new color(240,240,240)))); pdfcontentbyte canvas = writer.getdirectcontentunder(); pdfcontentbyte addwatermark; addwatermark=writer.getdirectcontentunder(); addwatermark.addimage(logo); pdfptable maintable = new pdfptable(2); pdfpcell spece = new pdfpcell(); spece.setborder(0); spece.setcolspan(2); spece.setfixedheight(20f); spece.setspacecharratio(20f); maintable.addcell(spece); image logo=image.getinstance("d:/logo_gmcmanin.png"); paragraph ph = new paragraph(); ph.add(new chunk(logo, 0, 0)); ph.add(new chunk("\n\nguwahati municipal corporation", fontfactory.getfont(fontfactory.times_roman, 10, font.bolditalic))); ph.add(new chunk("\n\nmoney recipt", fontfactory.getfont(fontfactory.times_roman, 10, font.bold))); pdfpcell heading = new pdfpcell(ph); // heading.setborder(1); heading.setcolspan(2); heading.sethorizontalalignment(element.align_center); heading.setverticalalignment(element.align_center); maintable.addcell(heading); paragraph ph2 = new paragraph(); ph2.add(new chunk("\npayment receipt / acknowledgment property tax bill of ", fontfactory.getfont(fontfactory.times_roman, 10, font.bolditalic))); ph2.add(new chunk("\n\nfinancial year 2014-15", fontfactory.getfont(fontfactory.times_roman, 10, font.bold))); pdfpcell reciptheading = new pdfpcell(ph2); // heading.setborder(1); reciptheading.setcolspan(2); reciptheading.sethorizontalalignment(element.align_center); reciptheading.setverticalalignment(element.align_center); maintable.addcell(reciptheading); paragraph ph3 = new paragraph(); ph3.add(new chunk("receipt no ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); ph3.add(new chunk(" 2014-15", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); pdfpcell reciptcell=new pdfpcell(ph3); //reciptcell.setborder(0); //reciptcell.setborderwidthleft(1); reciptcell.setborderwidthbottom(0); reciptcell.setborderwidthtop(0); reciptcell.setborderwidthright(0); reciptcell.setcolspan(1); reciptcell.sethorizontalalignment(element.align_center); reciptcell.setverticalalignment(element.align_center); maintable.addcell(reciptcell); paragraph ph4 = new paragraph(); ph4.add(new chunk("date ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); ph4.add(new chunk("2014-15", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); pdfpcell datecell=new pdfpcell(ph4); datecell.setfixedheight(20); //datecell.setborder(0); datecell.setborderwidthleft(0); datecell.setcolspan(1); datecell.setborderwidthtop(0); datecell.setborderwidthbottom(0); datecell.sethorizontalalignment(element.align_center); datecell.setverticalalignment(element.align_center); maintable.addcell(datecell); pdfpcell details = new pdfpcell(); //details.setborder(0); details.setcolspan(2); pdfptable detailstable = new pdfptable(2); detailstable.setwidthpercentage(65); pdfpcell ownercell=new pdfpcell(new phrase("owner’s name ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); ownercell.sethorizontalalignment(element.align_left); detailstable.addcell(ownercell); pdfpcell ownernamecell=new pdfpcell(new phrase("chandan sarma ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); ownernamecell.sethorizontalalignment(element.align_right); detailstable.addcell(ownernamecell); pdfpcell assementidoldcell=new pdfpcell(new phrase("assessment id (old) ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); assementidoldcell.sethorizontalalignment(element.align_left); detailstable.addcell(assementidoldcell); pdfpcell assementidvaluecell=new pdfpcell(new phrase("27-05-7859 ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); assementidvaluecell.sethorizontalalignment(element.align_right); detailstable.addcell(assementidvaluecell); pdfpcell assementidnewcell=new pdfpcell(new phrase("assessment id (new) ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); assementidoldcell.sethorizontalalignment(element.align_left); detailstable.addcell(assementidoldcell); pdfpcell assementidnewvaluecell=new pdfpcell(new phrase("225642 ", fontfactory.getfont(fontfactory.times_roman, 8, font.normal))); assementidnewvaluecell.sethorizontalalignment(element.align_right); detailstable.addcell(assementidnewvaluecell); details.setborderwidthtop(0); details.setborderwidthbottom(0); details.addelement(detailstable); maintable.addcell(details); paragraph ph6=new paragraph(); ph6.add(new chunk("household waste collection ", fontfactory.getfont(fontfactory.times_roman, 8, font.bolditalic,color.red))); ph6.add(new chunk("\n\nward-wise contact number of ngos collecting household waste doorstep:- ", fontfactory.getfont(fontfactory.times_roman, 6))); ph6.add(new chunk("\n\nward no. 1- 9957047867 ward no. 17- 9864623744", fontfactory.getfont(fontfactory.times_roman, 6))); ph6.add(new chunk("\n\nward no. 1- 9957047867 ward no. 17- 9864623744", fontfactory.getfont(fontfactory.times_roman, 6))); ph6.add(new chunk("\n\nward no. 1- 9957047867 ward no. 17- 9864623744", fontfactory.getfont(fontfactory.times_roman, 6))); ph6.add(new chunk("\n\nward no. 1- 9957047867 ward no. 17- 9864623744", fontfactory.getfont(fontfactory.times_roman, 6))); ph6.add(new chunk("\n\nward no. 1- 9957047867 ward no. 17- 9864623744", fontfactory.getfont(fontfactory.times_roman, 6))); pdfpcell wastecollectionheader=new pdfpcell(ph6); wastecollectionheader.setcolspan(1); wastecollectionheader.sethorizontalalignment(element.align_center); maintable.addcell(wastecollectionheader); paragraph ph7=new paragraph(); ph7.add(new chunk("property tax related grievance redressal mechanism ", fontfactory.getfont(fontfactory.times_roman, 8, font.bolditalic,color.red))); ph7.add(new chunk("\n\nfor billing or service grievance, please approach (during office hours) ", fontfactory.getfont(fontfactory.times_roman, 6))); ph7.add(new chunk("\n\n •deputy commissioner, east zone, gmc, bhaya mama path, rg baruah road, mobile no: 07399092259 ", fontfactory.getfont(fontfactory.times_roman, 6))); ph7.add(new chunk("\n\n •deputy commissioner, east zone, gmc, bhaya mama path, rg baruah road, mobile no: 07399092259 ", fontfactory.getfont(fontfactory.times_roman, 6))); ph7.add(new chunk("\n\n •deputy commissioner, east zone, gmc, bhaya mama path, rg baruah road, mobile no: 07399092259 ", fontfactory.getfont(fontfactory.times_roman, 6))); ph7.add(new chunk("\n\n •deputy commissioner, east zone, gmc, bhaya mama path, rg baruah road, mobile no: 07399092259 ", fontfactory.getfont(fontfactory.times_roman, 6))); ph7.add(new chunk("\n\n •deputy commissioner, east zone, gmc, bhaya mama path, rg baruah road, mobile no: 07399092259 ", fontfactory.getfont(fontfactory.times_roman, 6))); pdfpcell propertytexrelatedheader=new pdfpcell(ph7); propertytexrelatedheader.setcolspan(1); propertytexrelatedheader.sethorizontalalignment(element.align_center); maintable.addcell(propertytexrelatedheader); m_pdfdocument.add(maintable); m_pdfdocument.newpage(); m_pdfdocument.setpagesize(m_pdfdocument.getpagesize()); m_pdfdocument.close(); } catch(exception e) { system.out.println("exception is"+e.tostring()); } } }
add following lines of code when declaring pdf writer
pdfwriter writer = pdfwriter.getinstance(m_pdfdocument, new fileoutputstream("d:/aa.pdf")); m_pdfdocument.open(); phrase watermarkdd = new phrase("water mark name-", new font(fontfactory.getfont(fontfactory.helvetica, 20,font.normal,new color(240,240,240)))); pdfcontentbyte canvas = writer.getdirectcontentunder(); columntext.showtextaligned(canvas, element.align_center, watermarkdd, 298, 421, 45); ----your remaining code goes here----------
Comments
Post a Comment