27

Can't seem to find much out there for this. I've a PDF, onto which I'd like to overlay an image of an electronic signature. Any suggestions on how to accomplish that using PDFSharp?

Thanks

user948060
  • 953
  • 3
  • 12
  • 25
  • 1
    Are you looking to add a water mark or just an image in the text? – Kami Sep 17 '13 at 16:26
  • Kami, I'm looking to add a JPG image that I've rendered from SignaturePad, where the user electronically signed their name. I want to overlay the signature in the correct spot on the PDF. – user948060 Sep 18 '13 at 02:11

3 Answers3

43

Try the following

private void GeneratePDF(string filename, string imageLoc)
{
    PdfDocument document = new PdfDocument();

    // Create an empty page or load existing
    PdfPage page = document.AddPage();

    // Get an XGraphics object for drawing
    XGraphics gfx = XGraphics.FromPdfPage(page);
    DrawImage(gfx, imageLoc, 50, 50, 250, 250);

    // Save and start View
    document.Save(filename);
    Process.Start(filename);
}

void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
    XImage image = XImage.FromFile(jpegSamplePath);
    gfx.DrawImage(image, x, y, width, height);
}

This will generate a new PDF with the specified image near the top of the page. If you need to use an existing document change the PdfDocument constructor to

PdfDocument document = new PdfDocument(filename);

where filename is the name of the file to load and change the PdfPage line to

PdfPage page = document.Pages[pageNum];

where pageNum is the number of the page on which you need to add the image.

After that, it just a matter of getting the positioning on the page by altering the parameters for DrawImage to suit.

DrawImage(gfx, imageLoc, 50, 50, 250, 250);

Good luck!

Kami
  • 19,134
  • 4
  • 51
  • 63
  • When calling XGraphics.FromPdfPage you can specify whether the new drawing instructions will prepend the existing ones or will be appended. If image and text overlap, using Prepend may be useful. The Watermark sample demonstrates Prepend and Append (with text, not images): http://www.pdfsharp.net/wiki/Watermark-sample.ashx – I liked the old Stack Overflow Sep 18 '13 at 09:10
  • omg. what about if the jpg file source is taken from My.Resource? I think there's no absolute path we could take em... – gumuruh May 29 '14 at 01:07
  • 3
    @gumuruh God has nothing to do with it. There are other methods available - perhaps XImage.FromGdiPlusImage() ? It allows `System.Drawing.Image` which can be loaded from a file or a stream etc. – Kami May 29 '14 at 10:05
  • for all the people who use this nice code and intend to load the loaded picture into another document or even delete it after the usage, please add `image.Dispose()` after the loading from file and drawing it to the pdf. – Mong Zhu Mar 03 '17 at 08:04
  • 1
    Adding to @MongZhu's comment--PdfDocument, XGraphics and XImage all support IDisposable (use "using "). – crokusek Jun 27 '18 at 01:06
  • Use XGraphics.DrawImage(image,0,0) to keep the size of the image. – FernandoG Dec 14 '21 at 00:19
8

This will help you:

    PdfDocument document = pdf;

    // Create a new page        
    PdfPage page = document.Pages[0];
    page.Orientation = PageOrientation.Portrait;

    XGraphics gfx = XGraphics.FromPdfPage(page, XPageDirection.Downwards);

    // Draw background
    gfx.DrawImage(XImage.FromFile("pdf_overlay.png"), 0, 0);

Just add the path to the image you want, and specify the position of the image.

ffffff01
  • 5,068
  • 11
  • 52
  • 61
2

To roughly maintain the aspect ratio, I used @Kami's answer and "roughly" centered it. The assumption made is that the pdf width is 600 and pdf height is 800, we will make use of the middle 500 and 700 of the page only, leaving the 4 sides have at least 50 in length as margin.

    public static void GeneratePdf(string outputPath, string inputPath)
    {
        PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();

        // Create an empty page or load existing
        PdfPage page = document.AddPage();

        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);
        DrawImage(gfx, inputPath);

        // Save and start View
        document.Save(outputPath);
        Process.Start(outputPath);
    }

    public static void DrawImage(XGraphics gfx, string imagePath)
    {
        XImage image = XImage.FromFile(imagePath);
        var imageHeight = image.PixelHeight;
        var imageWidth = image.PixelWidth;
        int height;
        int width;
        int x;
        int y;

        width = 500;
        height = (int) Math.Ceiling((double) width * imageHeight / imageWidth);

        x = 50;
        y = (int) Math.Ceiling((800 - height) / 2.0);
        
        if(height > 700)
        {
            height = 700;
            width = (int) Math.Ceiling(imageWidth * (double) height / imageHeight);

            y = 50;
            x = (int) Math.Ceiling((600 - width) / 2.0);
        }
        
        gfx.DrawImage(image, x, y, width, height);
    }
lrh09
  • 557
  • 4
  • 13