Opening an existing PDF and writing semi-transparent text on every page.
Example_48.pdf — the PDF this example creates.
package examples;
import java.io.*;
import java.util.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;
/**
* Example_48.java
*/
class Example_48 {
public Example_48(String fileName) throws Exception {
PDF pdf = new PDF(new BufferedOutputStream(new FileOutputStream("Example_48.pdf")));
List<PDFobj> objects = pdf.read(new FileInputStream(fileName));
// A font added to an existing PDF is embedded as a subset of the glyphs
// drawn, as in a new PDF. IBM Plex Sans, which PDFjet carries, is read
// from the library; the path of a font file of your own reads that file.
Font f1 = new Font(objects, IBMPlexSans.Regular);
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);
for (PDFobj pageObj : 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) throws Exception {
long time0 = System.currentTimeMillis();
new Example_48("resources/data/testPDFs/scanned-linearized.pdf");
long time1 = System.currentTimeMillis();
System.out.printf("Example_48 => %4d ms%n", time1 - time0);
}
} // End of Example_48.java
©2026 PDFjet Software.