Error using PDF sharp in BPM


I’m trying to add a watermark to a pdf that we are attaching in Epicor in a Method Directive. I’m attempting to use PDF sharp to do so. I’m getting an error saying FontFamily is defined in an assembly that is not referenced.

I’ve tried adding a using statement referencing System.Drawing. I’ve added System.Drawing.Common as reference. Not sure if anyone has any ideas on how to get this error to go away. I’ve also tried the same exact code in a customization and it works with no errors. Here is my code below, it is the XFont line that has the error

        PdfDocument document = PdfReader.Open(mytempfile, PdfDocumentOpenMode.Modify);      

            // Create font and brush for the watermark
            XFont font = new XFont("Arial",40,XFontStyle.Bold);//"Arial", 40, XFontStyle.Bold);
            XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0)); // Semi-transparent red
            // Add watermark to each page
            foreach (PdfPage page in document.Pages)
                XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
                gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                gfx.DrawString("Watermark", font, brush, 0, 0, XStringFormats.Center);


Pop the code in here so we can get some deets.

Apparently I was too quick.

Too Fast Tempo GIF by DrSquatchSoapCo


You’re just too fast.
Looney Tunes Running GIF by Looney Tunes World of Mayhem

1 Like

Try using a more modern version of that library that references the newer System.Drawing.

With 10.2.7? I doubt that, maybe it should be opposite

1 Like

Didn’t see that.

But surely not old as ?

1 Like

Yep, it appears I’m using an old version of pdf sharp (version 1.50). I will try using a newer version and see if that fixes the error. It does make me wonder why the same code works in a customization… Appreciate the quick responses!

We did this at my last job, I’m fairly certain we were able to use the same version of PDFSharp on 10.2.700 and 2023.1. I’ll get version numbers for you in a minute, and share the code we’re using for the watermark.

Here’s the relevant bit of the function we used to add watermarks using pdfSharp.

// Get bkg image, set properties
	var FormImage = XImage.FromFile ( @"C:\Path\To\Pdf.pdf" );
	var width   = FormImage.PixelWidth * 72 / FormImage.HorizontalResolution;
	var height  = FormImage.PixelHeight * 72 / FormImage.VerticalResolution;

// Draw watermark to SSRS report
	var source  = PdfReader.Open ( pdfName );
	var gfx = XGraphics.FromPdfPage( source.Pages[0], XGraphicsPdfPageOptions.Prepend );

	gfx.DrawImage ( FormImage, 0, -180, width, height );
	source.Save ( pdfName );


Thank you. I was going to add the watermark as an image which would work, but I was having issues getting it transparent.

Oh I see. I think we just made the source image more transparent to get around that. I did not try the way you were doing it, and that wouldn’t work if the watermark is not static.