简体   繁体   中英

Insert a picture on the top of each page in an OpenXML document

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.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM