Example_37

Opening an existing PDF and writing semi-transparent text on every page.

Example_37.pdf — the PDF this example creates.

using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using PDFjet.NET;

/**
 * Example_37.cs
 */
class Example_37 {
    public Example_37(String fileName) {
        PDF pdf = new PDF(new BufferedStream(
                new FileStream("Example_37.pdf", FileMode.Create)));
        List<PDFobj> objects = pdf.Read(new FileStream(fileName, FileMode.Open, FileAccess.Read));

        Font f1 = new Font(objects,
                new FileStream(IBMPlexSans.Regular,
                FileMode.Open,
                FileAccess.Read));
        f1.SetSize(72f);

        TextLine text = new TextLine(f1, "This is a test!");
        text.SetLocation(84f, 350f);    // the T lines up with the scanned text
        text.SetTextColor(Color.peru);

        List<PDFobj> pages = pdf.GetPageObjects(objects);
        foreach (PDFobj pageObj in pages) {
            GraphicsState gs = new GraphicsState();
            gs.SetAlphaStroking(0.75f);         // Stroking alpha
            gs.SetAlphaNonStroking(0.75f);      // Non-stroking alpha
            pageObj.SetGraphicsState(gs, objects);

            Page page = new Page(pdf, pageObj);
            page.AddResource(f1, objects);
            page.SetBrushColor(Color.blue);
            // page.DrawString(f1, "Hello, World!", 50f, 200f);
            text.DrawOn(page);

            page.Complete(objects); // The graphics stack is unwinded automatically
        }
        pdf.AddObjects(objects);

        pdf.Complete();
    }

    public static void Main(String[] args) {
        Stopwatch sw = Stopwatch.StartNew();
        long time0 = sw.ElapsedMilliseconds;
        new Example_37("data/testPDFs/scanned-linearized.pdf");
        long time1 = sw.ElapsedMilliseconds;
        Console.WriteLine($"Example_37 => {time1 - time0,4} ms");
    }
}   // End of Example_37.cs

©2026 PDFjet Software.