public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
string fileName = "TestFile.doc";
// This a document that we want to add an image and custom text for each page without using the header or footer.
Document doc = new Document(dataDir + fileName);
// Create and attach collector before the document before page layout is built.
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Images in a document are added to paragraphs, so to add an image to every page we need to find at any paragraph
// Belonging to each page.
IEnumerator enumerator = doc.SelectNodes("// Body/Paragraph").GetEnumerator();
// Loop through each document page.
for (int page = 1; page <= doc.PageCount; page++)
{
while (enumerator.MoveNext())
{
// Check if the current paragraph belongs to the target page.
Paragraph paragraph = (Paragraph)enumerator.Current;
if (layoutCollector.GetStartPageIndex(paragraph) == page)
{
AddImageToPage(paragraph, page, dataDir);
break;
}
}
}
// Call UpdatePageLayout() method if file is to be saved as PDF or image format
doc.UpdatePageLayout();
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
doc.Save(dataDir);
Console.WriteLine("\nInserted images on each page of the document successfully.\nFile saved at " + dataDir);
}