Q: I want to take 2 random images and place them underneath all of the
existing items on a pdf.
How would you do this? Do you have a sample? I am able to insert an
image with opacity, but I need the other objects to draw in top of it.
(a Z-order function?)
-----
A: To insert new content behind all existing content on PDF page pass
‘true’ as the second parameter in the call to writer.Begin(page,
false) method. For example:
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
// (as a background layer). To write in foreground remove
// the second parameter.
writer.Begin(page, true);
// 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);
}