替换第二页标题中的图像

替换第二页标题中的图像

本文介绍了Word,替换第二页标题中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word文档,该文档的第一页为标头,第二页和第二页为标头.两个标题中的每一个都包含一个图像.这些图像必须更换.

这段代码可以很好地删除所有图像.

I have a Word document with a header for the first page and a header for the 2nd and next pages. Each of the two headers contain an image. These images must be replaced.

This piece of code works fine to remove all images.

foreach (Word.Section section in objDocument.Sections)
{
  foreach (Word.HeaderFooter headerfooter in section.Headers)
  {
    Word.Shapes shapes = headerfooter.Shapes;
    for (int i = 0; i < shapes.Count; i++)
    {
      object item = i + 1;
      shape = shapes.get_Item(ref item);
      if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoEmbeddedOLEObject)
      {
        shapes.AddPicture(workparams.imagepath, ref linktofile, ref savewithdocument,
          ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

        shape.Delete();
      }
    }
  }
}



但是我添加的所有图像都显示在第一页的标题中.

它可能与图片的锚点"有关,但我无法弄清楚在哪里可以找到合适的锚点.

有人知道在第二页的页眉中插入图像的技巧吗?
谢谢...



But the images I add all show up in the header of the first page.

It probably has to do with the ''anchor'' of the image, but I can''t figure it out where to find the right anchor.

Does anybody know the trick to insert an image in the header on the second page?
Thanks...

推荐答案

Word.HeaderFooter headerfooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];



根据 MSDN [ ^ ] wdHeaderFooterFirstPage索引标头wdHeaderFooterPrimary访问该部分其余页面的标题.



According to MSDN[^] wdHeaderFooterFirstPage indexes the header on the first page in the section whilst wdHeaderFooterPrimary accesses the header on the remaining pages in the section.


foreach (Word.Section section in objDocument.Sections)
{
  headerfooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage];
  try
  {
    for (int i = 0; i < headerfooter.Range.InlineShapes.Count; i++)
    {
      inlineshape = headerfooter.Range.InlineShapes[i + 1];
      inlineshape.Delete();
      headerfooter.Range.InlineShapes.AddPicture(workparams.imagepath, ref linktofile, ref savewithdocument, ref Unknown);
    }
  }
  catch (Exception ex)
  {
    throw;
  }


  headerfooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
  try
  {
    for (int i = 0; i < headerfooter.Range.InlineShapes.Count; i++)
    {
      inlineshape = headerfooter.Range.InlineShapes[i + 1];
      inlineshape.Delete();
      headerfooter.Range.InlineShapes.AddPicture(workparams.imagepath, ref linktofile, ref savewithdocument, ref Unknown);
    }
  }
  catch (Exception ex)
  {
    throw;
  }
}


这篇关于Word,替换第二页标题中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:54