C# turn transparency into white for a .pdf document

493 views Asked by At

I have a .pdf document that has transparency in its pages. I need to remove that transparency and make it white, how can I do that with C#? I can use pdftron, itextsharp or any other free library.

1

There are 1 answers

0
Shakthi Wijeratne On

Using PDFTrons PDFNet SDK, you can insert a white rectangle as the background to remove the default page transparency as suggested by @mkl. Below is an example using the ElementBuilder class. You can look at the ElementBuilder sample code for more information.

using (PDFDoc doc = new PDFDoc(@"D:\in.pdf"))
using (ElementBuilder eb = new ElementBuilder())
using (ElementWriter writer = new ElementWriter())
{
    int pagenum = 1;
    writer.Begin(doc.GetPage(pagenum), ElementWriter.WriteMode.e_underlay);
    Element e = eb.CreateRect(0, 0, doc.GetPage(pagenum).GetPageWidth(), doc.GetPage(pagenum).GetPageHeight());
    e.SetPathFill(true);
    e.SetPathStroke(true);
    e.SetPathClip(false);
    e.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
    e.GetGState().SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
    e.GetGState().SetStrokeColor(new ColorPt(255, 255, 255)); // white background fill color 
    e.GetGState().SetFillColor(new ColorPt(255, 255, 255)); // stroke color white as well
    writer.WritePlacedElement(e);
    writer.End();

    doc.Save(@"D:\output.pdf", 0);
}