Different ways of adding your signature on a PDF

last year -  

My coworker wrote some Python code to add a signature on PDFs. Since Python isn’t my thing, I wrote the same code in a few of my favorite languages. Probably because I haven’t used Python in production or because of its wired syntax (Maybe I only like curly-brace languages). Anyway, here’s the Python code I mentioned:

import PyPDF2
from fpdf import FPDF
import time

output_file = "merged.pdf"
watermark_file = "converted.pdf"

def convert_to_pdf():
    path_to_image = input('image to add: ')
    pdf = FPDF()
    pdf.image(path_to_image,130,270, 75, 25)

def merge_pdf():
    input_file = input('Path to target pdf: ')
    with open(input_file, "rb") as filehandle_input:
    # read content of the original file
        pdf = PyPDF2.PdfFileReader(filehandle_input)

        with open(watermark_file, "rb") as filehandle_watermark:
            # read content of the watermark
            watermark = PyPDF2.PdfFileReader(filehandle_watermark)

            # get first page of the original PDF
            first_page = pdf.getPage(0)

            # get first page of the watermark PDF
            first_page_watermark = watermark.getPage(0)

            # merge the two pages

            # create a pdf writer object for the output file
            pdf_writer = PyPDF2.PdfFileWriter()

            # add page

            with open(output_file, "wb") as filehandle_output:
                # write the watermarked file to the new file


There’s nothing complicated about the code. A PDF is created by converting the image into a PDF and merging it with the PDF that needs to have a signature.


I will start with the PowerShell version, I have used iTextSharp, which is a .NET library to work with PDFs. The code is pretty straightforward:

Add-Type -Path "*.dll"
$pdf = [iTextSharp.text.pdf.PdfReader]::new("$(Get-Location)/invoice.pdf")
$fs = [System.IO.FileStream]::new("$(Get-Location)/signed-invoice.pdf",
$stamper = [iTextSharp.text.pdf.PdfStamper]::new($pdf, $fs)
$content = $stamper.GetOverContent(1)
$width = $pdf.GetPageSize(1).Width
$image = [iTextSharp.text.Image]::GetInstance("$(Get-Location)/sign.jpg")
$image.SetAbsolutePosition($width - 130, 50)
$image.ScaleAbsolute(100, 30)


The C# version is almost the same as the PowerShell one. I have used the same library, iTextSharp:

using iTextSharp.text;
using iTextSharp.text.pdf;

var pdfReader = new PdfReader("../invoice.pdf");
var outStream = new FileStream("../signed-invoice.pdf", FileMode.Create);
var pdfStamper = new PdfStamper(pdfReader, outStream);
var content = pdfStamper.GetOverContent(1);
var width = pdfReader.GetPageSize(1).Width;
var baseDir = AppDomain.CurrentDomain.BaseDirectory.Split(new[] { "bin" }, StringSplitOptions.None)[0];
var signFile = Path.Combine(baseDir, "..", "sign.jpg");
var image = Image.GetInstance(signFile);
image.SetAbsolutePosition(width - 130, 50);
image.ScaleAbsolute(100, 30);

Both versions use the PdfReader class to open a PDF file, then use PdfStamper to create a new PDF file. PdfStamper adds content to an existing PDF. Then we have added the image to the PDF using the AddImage method.


As you can see the Golang version is pretty short. It uses the pdft library to add the signature to the PDF. I could have removed the error handling to make it even shorter.

package main

import (


func main() {
	var pt pdft.PDFt
	err := pt.Open("../invoice.pdf")
	if err != nil {
		panic("Couldn't open pdf.")
	pic, err := ioutil.ReadFile("../sign.jpg")
	if err != nil {
		panic("Couldn't read pic.")
	err = pt.InsertImg(pic, 1, 1950.0, -2450.0, 400, 100)
	if err != nil {
		panic("Couldn't insert image")
	err = pt.Save("../signed-invoice.pdf")
	if err != nil {
		panic("Couldn't save pdf.")


The Perl version is also pretty short. It uses the PDF::API2 library to add the signature to the PDF:

use PDF::API2;
my $pdf = PDF::API2->open('../invoice.pdf');
my $img = $pdf->image_jpeg('../sign.jpg');
my $page = $pdf->openpage(1);
my $gfx = $page->gfx;
$gfx->image($img, 1950, 3200, 400, 100);


The Bash version is the shortest one. It uses the pdftk command to add the signature to the PDF. The Bash version’s signature must be in PDF format rather than an image, which means that it overlays the stamp PDF page on top of the input PDF document’s pages, like a background:

pdftk ./invoice.pdf stamp ./a4-sign.pdf output signed-invoice.pdf