Outputted pdf not filling with the content in iTextsharp and C#.net, creating with Zero bytes -
i working replace specific word inside pdf document using itextsharp , c#.net, while debugging getting proper value outputted pdf getting 0 bytes(empty),its not filling content.
replacepdftext("mumbai",stringcomparison.currentcultureignorecase,application.startuppath + "\\test.pdf","d:\\test_words_replaced.pdf"); //do public void replacepdftext(string strsearch, stringcomparison sccase, string strsource, string strdest) { pdfstamper psstamp = null; //pdf stamper object pdfcontentbyte pcbcontent = null; //read pdf content if (file.exists(strsource)) //check if file exists { pdfreader pdffilereader = new pdfreader(strsource); //read our file psstamp = new pdfstamper(pdffilereader, new filestream(strdest, filemode.create)); //read underlying content of pdf file pbprogress.value = 0; //set progressbar minimum value pbprogress.maximum = pdffilereader.numberofpages; //set progressbar maximum value (int intcurrpage = 1; intcurrpage <= pdffilereader.numberofpages; intcurrpage++) //loop through pages { loctextextractionstrategy ltestrategy = new loctextextractionstrategy(); //read pdf file content blocks pcbcontent = psstamp.getundercontent(intcurrpage); //look @ current block //determine spacing of block see if matches our search string ltestrategy.undercontentcharacterspacing = pcbcontent.characterspacing; ltestrategy.undercontenthorizontalscaling = pcbcontent.horizontalscaling; //trigger block reading process string currenttext = pdftextextractor.gettextfrompage(pdffilereader, intcurrpage, ltestrategy); //determine match(es) list<itextsharp.text.rectangle> lstmatches = ltestrategy.gettextlocations(strsearch, sccase); pdflayer pdlayer = default(pdflayer); //create new layer pdlayer = new pdflayer("overrite", psstamp.writer); //enable overwriting capabilities //set fill colour of replacing layer pcbcontent.setcolorfill(basecolor.black); foreach (itextsharp.text.rectangle rctrect in lstmatches) //loop through each match { pcbcontent.rectangle(rctrect.left, rctrect.bottom, rctrect.width, rctrect.height); //create new rectangle replacing layer pcbcontent.fill(); //fill colour specified pcbcontent.beginlayer(pdlayer); //create layer pcbcontent.setcolorfill(basecolor.black); //fill alyer pcbcontent.fill(); //fill underlying content pdfgstate pgstate = default(pdfgstate); //create gstate object pgstate = new pdfgstate(); pcbcontent.setgstate(pgstate); //set current state pcbcontent.setcolorfill(basecolor.white); //fill letters pcbcontent.begintext(); //start text replace procedure pcbcontent.settextmatrix(rctrect.left, rctrect.bottom); //get text location //set new font , size pcbcontent.setfontandsize(basefont.createfont(basefont.helvetica, basefont.cp1252, basefont.not_embedded), 9); pcbcontent.showtext("amazing!!!!"); //replacing text pcbcontent.endtext(); //stop text replace procedure pcbcontent.endlayer(); //stop layer replace rocedure } pbprogress.value++; //increase progressbar value pdffilereader.close(); //close file } //psstamp.close(); //close stamp object } }
you call
pdffilereader.close();
much early: inside loop in next iteration still requires pdffilereader
, furthermore before closing stamper.
the stamper requires pdfreader
still open when stamper closes because copies parts of reader then.
Comments
Post a Comment