Set PDF Background as Transparent in order to add a background image

Hello Everyone,
I’m looking for a way to perform the request of a customer but it seems impossible to find a solution.
I have a PDF generated by a third party software and I have to add a background template.to it.
I have successfully added my background image behind each page of my pdf but I realized that my source pdf has a white background and not a transparent background.
So I can see the very left/right/top/bottom pixels of my backgound but everything is still with a white background.
Is there any way to add a backgound to my original PDF, replacing the white background ?
Any help is fully welcome. Thanks in advance.
Kind regards,

Hi David,

Can you send us your PDF file so we can investigate that white background element?

Regards,
Mario

Hi Mario,
Thanks for your message. How do you want me to share the PDF ?
Regards,
David

You can send it to us via email or support ticket, see the Contact page.

The problem was that some elements had a white fill color, which overlapped with the background image.

The solution was:

using var document = PdfDocument.Load("MainDocument.pdf");
var backgroundImage = PdfImage.Load("Background.jpeg");

foreach (var page in document.Pages)
{
    var elements = page.Content.Elements;

    // Remove white background color from path elements.
    foreach (var path in elements.All().OfType<PdfPathContent>())
    {
        var fill = path.Format.Fill;
        if (fill.IsApplied && fill.Color.TryGetRgb(out double r, out double g, out double b) && r == 1 && g == 1 && b == 1)
            fill.IsApplied = false;
    }

    // If needed, apply mask to remove white color from image elements.
    foreach (var imageSrc in elements.All().OfType<PdfImageContent>().Select(i => i.Image))
    {
        var imageDic = imageSrc.GetDictionary();
        if (imageDic.ContainsKey(PdfName.Create("Decode")) || imageDic.ContainsKey(PdfName.Create("Mask")))
            continue;

        int valueCount = imageSrc.ColorSpace == PdfColorSpace.DeviceRGB ? 6 :
            imageSrc.ColorSpace == PdfColorSpace.DeviceGray ? 2 : 0;

        if (valueCount == 0)
            continue;

        int value = (1 << imageSrc.BitsPerComponent.Value) - 1;
        var array = PdfArray.Create(Enumerable.Repeat(PdfNumber.Create(value), valueCount));

        for (int i = 0; i < valueCount; i += 2)
            array[i] = PdfNumber.Create(((PdfNumber)array[i]).Value * 0.90);

        imageDic[PdfName.Create("Mask")] = array;
    }

    var group = elements.AddGroup(elements.First);
    var backgroundImageElement = group.Elements.AddImage(backgroundImage);
    backgroundImageElement.Transform = new PdfMatrix(page.CropBox.Width, 0, 0, page.CropBox.Height, 0, 0);
}

document.Save("MainDocument-With-Background.pdf");

Or in case a background PDF file is used, instead of a background JPG image:

// Replace this:
//var backgroundImage = PdfImage.Load("Background.jpeg");

// With this:
using var backgrounDocument = PdfDocument.Load("Background.pdf");
var backgroundPage = backgrounDocument.Pages[0];
var backgroundForm = backgroundPage.ConvertToForm(document);


// ...

// Replace this:
//var group = elements.AddGroup(elements.First);
//var backgroundImageElement = group.Elements.AddImage(backgroundImage);
//backgroundImageElement.Transform = new PdfMatrix(page.CropBox.Width, 0, 0, page.CropBox.Height, 0, 0);

// With this:
var group = elements.AddGroup(elements.First);
group.Elements.AddForm(backgroundForm);

Regards,
Mario

Thanks again Mario. This piece of code is working like a charm.