Aspose – Add Base64 Image Stamp to PDF Footer

The exercise shows how you can add a base64 encoded image to a PDF footer. I have used Aspose library for PDF manipulations. It will be like

  1. takes the base64 signature and convert that string into a byte
  2. Keep that byte into the memory stream
  3. Stamp it on pdf footer
  4. Save PDF

Nuget Package for aspose: Install-Package Aspose.Pdf -Version 19.1.0
https://www.nuget.org/packages/Aspose.PDF/

Sample Code


byte[] bytes = Convert.FromBase64String("A Valid BASE64 String");
System.Drawing.Image image;
MemoryStream ms = new MemoryStream(bytes); //image stream
//image = System.Drawing.Image.FromStream(ms); //we don't need this

Document pdfDocument = new Document("empty.pdf");
var imageStream = File.Open("sampleImage.png", FileMode.Open);

// Create footer

//For Text Stamp
TextStamp textStamp = new TextStamp("You are here....");
// Set properties of the text stamp
textStamp.BottomMargin = 10;
textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;

// FOR IMAGE Stamp
MemoryStream memoryStream = new MemoryStream(bytes);

ImageStamp imageStamp = new ImageStamp(ms);
// Set properties of the image stamp
imageStamp.BottomMargin = 10;
imageStamp.RightMargin = 130;
imageStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
imageStamp.VerticalAlignment = VerticalAlignment.Bottom;
imageStamp.Height = 70;
imageStamp.Width = 70;

// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(imageStamp);
}

pdfDocument.Save("footer.pdf");

Leave a Reply

Your email address will not be published. Required fields are marked *