A: The problem seems to be related to positioning of the image within
the annotation bounding box (there was another error where you were
writing the appearance stream on the page instead of the separate
const stream). In any case you can find all the modifications and a
working test project in the attachment.
using System;
using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;
using System.Collections;
namespace SpecPrinting
{
/// <summary>
/// Make a multi-page PDF using an existing PDF as a template.
/// </summary>
class Class1
{
static void Main(string[] args)
{
PDFNet.Initialize();
PDFNet.SetResourcesPath("../../../../../Resources");
// Relative path to the folder containing test files.
string input_path = "D:\\_TESTME\\";
string output_path = "D:\\_TESTME\\";
Int32 pageCount = 1;
try
{
PDFDoc template = new PDFDoc(input_path + "05ML.pdf");
template.InitSecurityHandler();
PDFDoc new_PDF = new PDFDoc();
//Create as many pages as we need...
for (int loop = 1; loop <= pageCount; loop++)
{
new_PDF.PagePushBack(template.GetPage(1));
}
// Rename fields as 'fieldname_pagecount ie: Text1_1
RenameAllFields(new_PDF, pageCount);
//Set field values...
Field fld = null;
for (int loop = 1; loop <= pageCount; loop++)
{
fld = new_PDF.GetField("CUSTOMER_DESCR_" + loop.ToString());
if (fld != null)
{
fld.SetValue("LABATT BREWERIES OF CANADA");
}
fld = new_PDF.GetField("CUSTPROD_" + loop.ToString());
if (fld != null)
{
fld.SetValue("8300 710 ML BUD");
}
// You get the idea...
// Regenerate field appearances otherwise we won't see field
values...
new_PDF.RefreshFieldAppearances();
#region Insert design spec PDF on a Button
Annot button = new
Annot(new_PDF.GetField("DESSIN_1").GetSDFObj());
PDFDoc import_PDF = new PDFDoc(input_path +
"028550_000605_00003.pdf");
import_PDF.InitSecurityHandler();
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(new_PDF);
Page import_Page = import_PDF.GetPage(1);
Element element = builder.CreateForm(import_Page, new_PDF);
element.GetGState().SetTransform(1, 0, 0, 1, 0, 0);
writer.WritePlacedElement(element);
Obj appearance_stream = writer.End();
// Set the bounding box
Rect bbox = button.GetRect();
double sc_x = bbox.Width() / import_Page.GetPageWidth();
double sc_y = bbox.Height() / import_Page.GetPageHeight();
double scale = Math.Min(sc_x, sc_y);
appearance_stream.PutRect("BBox", 0, 0,
import_Page.GetPageWidth(), import_Page.GetPageHeight());
appearance_stream.PutMatrix("Matrix", new Matrix2D(scale, 0, 0,
scale, 0, 0));
appearance_stream.PutName("Subtype", "Form");
// Set the button appearance...
button.SetAppearance(appearance_stream);
#endregion
}
// Save new PDF...
new_PDF.Save(output_path + "Output.pdf",
SDFDoc.SaveOptions.e_linearized);
template.Close();
new_PDF.Close();
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
PDFNet.Terminate();
}
static void RenameAllFields(PDFDoc doc, String name)
{
Field fld = doc.GetField(name);
for (int counter = 1; fld != null; ++counter)
{
fld.Rename(name + "_" + counter.ToString());
fld = doc.GetField(name);
}
}
static void RenameAllFields(PDFDoc doc, Int32 count)
{
Hashtable FormFields = new Hashtable();
FieldIterator itr;
int hashIndex = 0;
// Load hash table to identify each unique field in the form...
for (itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next())
{
try
{
hashIndex = (int)FormFields[itr.Current().GetName()];
}
catch (NullReferenceException)
{
FormFields.Add(itr.Current().GetName(), 0);
}
}
// Enumerate the hash table and rename all occurrences via a
loop...
IDictionaryEnumerator hashEnumerator = FormFields.GetEnumerator();
while (hashEnumerator.MoveNext())
{
for (int loop = 1; loop <= count; loop++)
{
Field fld = doc.GetField(hashEnumerator.Entry.Key.ToString());
fld.Rename(hashEnumerator.Entry.Key.ToString() + "_" +
loop.ToString());
}
}
}
}
}