c# - ITextSharp/Pdftk: place Base64 Image from Web on PDF as Pseude-Signature -
i trying conceptualize way base64 image onto rendered pdf in itext. goal have pdf save disk reopen apply "signature" in right spot.
i haven't had success finding other examples online i'm asking stack.
my app uses .net c#.
any advice on how started?
as @mkl mentioned question confusing, title - base64 , signature not go together. guessing want place base64 image web on pdf pseudo signature?!?!
a quick working example started:
static void main(string[] args) { string currentdir = appdomain.currentdomain.basedirectory; // 'input' => rendered pdf in itext pdfreader reader = new pdfreader(input); string outputfile = path.combine(currentdir, output); using (var stream = new filestream(outputfile, filemode.create)) { using (pdfstamper stamper = new pdfstamper(reader, stream)) { acrofields form = stamper.acrofields; var fldposition = form.getfieldpositions("lname")[0]; rectangle rectangle = fldposition.position; string base64image = "data:image/png;base64,ivborw0kggoaaaansuheugaaaauaaaafcayaaacnbyblaaaaheleqvqi12p4//8/w38giaxdibke0dhxgljnbaao9txl0y4ohwaaaabjru5erkjggg=="; regex regex = new regex(@"^data:image/(?<mediatype>[^;]+);base64,(?<data>.*)"); match match = regex.match(base64image); image image = image.getinstance( convert.frombase64string(match.groups["data"].value) ); // best fit if image bigger form field if (image.height > rectangle.height || image.width > rectangle.width) { image.scaleabsolute(rectangle); } // form field top left - change parameters needed set different position image.setabsoluteposition(rectangle.left + 2, rectangle.top - 2); stamper.getovercontent(fldposition.page).addimage(image); } } } if you're not working pdf form template, (acrofields in code snippet) explicitly set absolute position , scale image needed.
Comments
Post a Comment