/* * Example_48.cs * * Copyright (c) 2026 PDFjet Software * Licensed under the MIT License. See LICENSE file in the project root. */ using System; using System.IO; using System.Collections.Generic; using System.Diagnostics; using PDFjet.NET; /** * Example_48.cs */ class Example_48 { public Example_48(String fileName) { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_48.pdf", FileMode.Create))); List objects = pdf.Read(new FileStream(fileName, FileMode.Open, FileAccess.Read)); // A font added to an existing PDF is embedded as a subset of the glyphs // drawn, as in a new PDF. Font f1 = new Font(objects, new FileStream("fonts/IBMPlexSans/IBMPlexSans-Regular.ttf", 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 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_48("data/testPDFs/scanned-linearized.pdf"); long time1 = sw.ElapsedMilliseconds; Console.WriteLine($"Example_48 => {time1 - time0,4} ms"); } } // End of Example_48.cs