Stamping PDF files using PDFNet SDK

Q: What are my options for stamping PDF files using PDFNet SDK?

I would like to add different types of watermarks to files generated
via PDFTron universal converter.


A: The FAQ (http://www.pdftron.com/pdfnet/faq.html) contains the
following entry: How do I stamp a page?

Using PDFNet you can place watermarks or append new content (such as
such as text, logo, or images) using ElementWriter and ElementBuilder
as illustrated in the following snippet:

PDFNet.Initialize();
try
{
PDFDoc doc = new PDFDoc(“my.pdf”);
doc.InitSecurityHandler();

ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();

// Get the first page
Page page = doc.GetPage(1);

// Begin writing to the page
writer.Begin(page);

// Begin writing a block of text
Element element = eb.CreateTextBegin(
Font.Create(doc,
Font.StandardType1Font.e_times_roman), 12);
writer.WriteElement(element);

string txt = “Hello World!”;
element = eb.CreateTextRun(txt);
// Scale-up text 5 times and shift it by (0,600)
element.SetTextMatrix(5, 0, 0, 5, 0, 600);
writer.WriteElement(element);

// Set the spacing between lines
element.GetGState().SetLeading(15);
writer.WriteElement(eb.CreateTextNewLine());

// Draw the same text string; this time stroked.
element = eb.CreateTextRun(txt);
GState gstate = element.GetGState();
gstate.SetTextRenderMode(
GState.TextRenderingMode.e_stroke_text);
gstate.SetCharSpacing(-1.25);
gstate.SetWordSpacing(-1.25);
writer.WriteElement(element);

// Finish the block of text
writer.WriteElement(eb.CreateTextEnd());
writer.End();

doc.Save(“out.pdf”, 0);
doc.Close();
}
catch (PDFNetException e) {
Console.WriteLine(e.Message);
}

The following code snippet illustrates how to stamp all pages in the
document with a “Hello World!” string.

PDFDoc doc = new PDFDoc(“in.pdf”);
doc.InitSecurityHandler();

ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();

PageIterator itr=doc.GetPageIterator();
for (; itr.HasNext(); itr.Next())
{
writer.Begin(itr.Current());
Element element = eb.CreateTextBegin(
Font.Create(doc,
Font.StandardType1Font.e_times_roman),64);
writer.WriteElement(element);
element = eb.CreateTextRun(“Hello World!”);
// Position the text run
element.SetTextMatrix(1, 0, 0, 1, 20, 20);
writer.WriteElement(element);
writer.WriteElement(eb.CreateTextEnd());
writer.End(); // Save the changes
}

doc.Save(“out.pdf”, 0);
doc.Close();For a longer code example, illustrating the use of
ElementBuilder and ElementWriter, please take a look at ElementBuilder
sample project.

Using PDFNet it is also possible to create watermark annotations using
the similar procedure as outlined above. You would use ElementBuilder/
ElementWriter to create new appearance stream and Annot class to
create the annotation object.

In the current version of PDFNet there is a new high-level utility
class called Stamper (attached is a sample project illustrating the
use of Stamper - http://groups.google.com/group/pdfnet-sdk/web/StamperTest.zip).
With stamper it is easy to add/remove/replace image, text, and PDF
watermarks on existing pages or documents. In case you need more power
and flexibility, then ElementBuilder & ElementWriter are the way to
go. For more information and examples of how to stamp pages using
ElementBuilder/ElementWriter you may want to search this forum (e.g.
see http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/1aad731dd9028adc
etc).