3

I am trying to conceptualize a way to get base64 image onto an already rendered PDF in iText. The goal is to have the PDF save to disk then reopen to apply the "signature" in the right spot.

I haven't had any success with finding other examples online so I'm asking Stack.

My app uses .net c#.

Any advice on how to get started?

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
MizAkita
  • 1,115
  • 5
  • 21
  • 52
  • 1
    Base64 is just a way to represent binary data, same as ASCII, UTF8, ZIP etc, so that's not too important. It'll be your job to turn that data into either a file-based image or a .Net `Image` object. Otherwise you've got two steps, create PDF and sign PDF, right? To start with, are you good with the PDF creation step or do you need help with that one? – Chris Haas May 14 '15 at 15:52
  • I am able to save pdf to file... but how to stamp it is what's confusing. Using MemoryStream it saves to my server downloads folder with no problem ( response.BinaryWrite(contents) ) however - reopening it then stamping it in with the image in itextsharp is where im lost. – MizAkita May 14 '15 at 15:57
  • I'm not really getting what you want to do. Ok, you have a PDF and want to stamp something on it. So far, so good, nothing complicated yet. But in what relation are *base64* and *signature* to that task? Do you want to stamp a signature? What kind of signature? An image of a handwritten one? Or an advanced or qualified one? And why *base64*? – mkl May 14 '15 at 20:03
  • i want to stamp a base64 image signature onto a specific spot on the pdf after it is rendered... or before. whatever is easiest. – MizAkita May 14 '15 at 20:16
  • What is a "base64 image signature"? – mkl May 15 '15 at 06:31
  • 1
    its an image from a signature on canvas... it saves as base64 image. – MizAkita May 15 '15 at 19:42
  • Ah, essentially merely an image. @kuujinbo demonstrated the techniques. – mkl May 16 '15 at 07:42

1 Answers1

2

As @mkl mentioned the question is a confusing, especially the title - usually base64 and signature do not go together. Guessing you want to place a base64 image from web on the PDF as a pseudo signature?!?!

A quick working example to get you started:

static void Main(string[] args)
{
    string currentDir = AppDomain.CurrentDomain.BaseDirectory;
    // 'INPUT' => already 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 than form field
            if (image.Height > rectangle.Height || image.Width > rectangle.Width)
            {
                image.ScaleAbsolute(rectangle);
            }
            // form field top left - change parameters as needed to set different position 
            image.SetAbsolutePosition(rectangle.Left + 2, rectangle.Top - 2);
            stamper.GetOverContent(fldPosition.page).AddImage(image);
        }
    }
}

If you're not working with a PDF form template, (AcroFields in code snippet) explicitly set the absolute position and scale the image as needed.

kuujinbo
  • 9,272
  • 3
  • 44
  • 57
  • You rock! This worked perfectly! Only thing is the positioning... It shows up on the bottom of the page to the left. Anyway to position it absolute? – MizAkita May 15 '15 at 21:19
  • 2
    @MizAkita - updated code snippet to position at top left instead of bottom left. To change the absolute position of the image, change the two parameters passed to image.SetAbsolutePosition() as commented in second to last line. – kuujinbo May 16 '15 at 02:07