Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add stamp layer to PDF document

Tags:

c#

.net

pdf

I'm looking for a way to add an extra layer to a PDF document. The layer should be on top of existing layers and should display a text I want to put on there, kind of like a watermark. At the moment we have a way of doing this, but this just adds the text onto the picture embedded in the PDF, that's not what I want. Anyone has any ideas if there are libraries (free ones would be great) which do this?

like image 294
Jasper Avatar asked Dec 08 '11 13:12

Jasper


People also ask

How do you add a stamp to a PDF?

Do one of the following: Choose Tools > Stamp > Custom Stamps. Choose Tools > Comment > Stamps > Custom Stamps > Manage Stamps.


1 Answers

We user MigraDoc,

http://www.pdfsharp.net/MigraDocOverview.ashx?AspxAutoDetectCookieSupport=1

More specifically the PdfSharp library in PdfSharp.dll,

PdfDocument doc = PdfReader.Open(pdf1Point4FileDataStream, PdfDocumentOpenMode.Modify)

foreach (PdfPage page in doc.Pages)
{
    page.Orientation = PdfSharp.PageOrientation.Portrait;
    var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append, XPageDirection.Downwards);

    gfx.DrawString(approvalWatermark, approvalFont, watermarkBrush, new XPoint((page.Width - maxWidth + approvalDiff) / 2 - space - moveLeft, page.Height / 2 - height1 - space), format);
}

Just a bit of code taken from our project, so it is a bit incomplete. Take a look at the library and classes, there will be some documentation around.

like image 159
peter Avatar answered Oct 06 '22 22:10

peter