0

I am creating OpenXML documents in C# for Word and I find always that it's not possible to determine where a page break will appears in the document. This creates the following problem:

I want to insert on the top of each page a little image, which gives a little overview of the elements of the page.

To that effect, is it possible to use conditions such as:

"if(page break reached == true) then insert a little image on the next page"?

I could use this condition after each paragraph, so I do not have to know where a page break appears. Any other solutions would also help.

joce
  • 9,624
  • 19
  • 56
  • 74
JonSmi
  • 1
  • 1
  • Or put the image in the header, the header HAS to appear at the top of each page –  Feb 14 '13 at 17:23
  • each image has specific informations about the elements of the page, so the images are different from page to page. – JonSmi Feb 14 '13 at 17:25
  • you can have different images in different headers –  Feb 14 '13 at 17:27

2 Answers2

0

Word documents are not paginated in the file format. The only way to determine to what objects are on what page is to use a rendering engine. Aspose.Words is one example but it's not cheap.

Another option is to add a header and put the image there or use a watermark

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
0

You can workaround your issue by manually inserting a page break whenever you want to insert an image, page break in xml,

 <w:r>
      <w:br w:type="page" />
 </w:r>

You also need to add lastRenderedPageBreak element before the content in your next page,

  <w:r>
    <w:lastRenderedPageBreak />
    <w:t>your content on page 2</w:t>
  </w:r>

The same thing can be acheived in code as:

    Run run1 = new Run();
    Break break1 = new Break(){ Type = BreakValues.Page }; //Breaks page
    run1.Append(break1); //append your run to paragraph on page 1

on page 2

    Run run2 = new Run();
    LastRenderedPageBreak lastRenderedPageBreak1 = new LastRenderedPageBreak();
    //add your image here in openxml code
    run2.Append(lastRenderedPageBreak1);
Flowerking
  • 2,551
  • 1
  • 20
  • 30