简体   繁体   English

在OpenXML文档的每页顶部插入图片

[英]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. 我正在C#for Word中创建OpenXML文档,但总是发现无法确定分页符将出现在文档中的何处。 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"? if(page break reached == true)然后在下一页插入一个小图像”吗?

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. Word文档未以文件格式分页。 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. Aspose.Words是一个例子,但它并不便宜。

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, 您可以通过以下方法来解决此问题:在您要插入图片时手动插入分页符,在xml中插入分页符,

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

You also need to add lastRenderedPageBreak element before the content in your next page, 您还需要在lastRenderedPageBreak内容之前添加lastRenderedPageBreak元素,

  <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 在第2页

    Run run2 = new Run();
    LastRenderedPageBreak lastRenderedPageBreak1 = new LastRenderedPageBreak();
    //add your image here in openxml code
    run2.Append(lastRenderedPageBreak1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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