Word文档多个背景



我有一个文件"test.docx",其中有3个页面部分,每个部分都与上一页取消了链接。现在我希望能够为每个部分设置不同的背景/水印。我只是不知道如何选择其他两个部分(或者…不是第一个)。

我这样试过:

Application nWord = new Application();
object oMissing = System.Reflection.Missing.Value;
object fileName = @"E:test.docx";
Document nDoc = nWord.Documents.Open(ref fileName, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Shape nShape = null;
for (int i = 0; i < nDoc.Sections.Count; i++)
{
    nShape =
    nDoc.Sections[i].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
    "TEXT" + i.ToString(), "Arial", (float)36, Microsoft.Office.Core.MsoTriState.msoTrue,
    Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, ref oMissing);
    nShape.Fill.Visible =
    Microsoft.Office.Core.MsoTriState.msoTrue;
    nShape.Line.Visible =
    Microsoft.Office.Core.MsoTriState.msoFalse;
    nShape.Fill.Solid();
    nShape.Fill.ForeColor.RGB = (Int32)WdColor.wdColorGray20;
    nShape.RelativeHorizontalPosition =
    WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
    nShape.RelativeVerticalPosition =
    WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
    nShape.Left = (float)WdShapePosition.wdShapeCenter;
    nShape.Top = (float)WdShapePosition.wdShapeCenter;
}
nWord.Visible = true;

但它只是在第一部分上去掉所有3个水印。

有什么想法吗?

WtBudgE

如果它把所有的水印都掉在同一个地方,那么这些部分可能是相连的。一种检查方法是打开Word文档并执行以下

打开页眉和页脚选项卡,在选项中,转到页眉和布局并选择"不同的首页"

完成后,现在测试您的代码。:)

您使用的是什么MS Word版本?也许,我可以发布一张相关的快照?

经过三天的搜索和讨论,我实际上找到了一个变通办法。这是我的解决方案:

//initialize
Application WordApp = new Application();
Document adoc = WordApp.Documents.Add();
Selection selection = adoc.ActiveWindow.Selection;
Shape wmShape;
object missing = System.Reflection.Missing.Value;
object linktofile = false;
object savewithdocument = true;
object CurrentPage = WdFieldType.wdFieldPage;
object TotalPages = WdFieldType.wdFieldNumPages;
//load background images
List<string> images = new List<string>();
images.Add(@"C:UsersPublicPicturesSample PicturesChrysanthemum.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesDesert.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesKoala.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesHydrangeas.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesJellyfish.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesPenguins.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesTulips.jpg");
images.Add(@"C:UsersPublicPicturesSample PicturesLighthouse.jpg");
//create all sections
object breaktype = WdBreakType.wdSectionBreakNextPage;
for (int i = 0; i < images.Count - 1; i++)
{
    adoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
    selection.InsertBreak(ref breaktype);
    adoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
    selection.HeaderFooter.LinkToPrevious = false;
}
//set background images
for (int i = 0; i < adoc.Sections.Count; i++)
{
    //select section header
    adoc.Sections[i+1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Select();
    //insert pagenumbers
    adoc.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
    selection.TypeText("Pagina ");
    selection.Fields.Add(selection.Range, ref CurrentPage, ref missing, ref missing);
    selection.TypeText(" van ");
    selection.Fields.Add(selection.Range, ref TotalPages, ref missing, ref missing);
    //insert shape
    wmShape = selection.InlineShapes.AddPicture(images[i], ref linktofile, ref savewithdocument).ConvertToShape();
    //modify shape properties
    wmShape.Select(ref missing);
    wmShape.Name = "WordPictureWatermark862903805";
    wmShape.PictureFormat.Brightness = (float)0.5;
    wmShape.PictureFormat.Contrast = (float)0.5;
    wmShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoFalse;
    wmShape.Height = WordApp.InchesToPoints((float)11.7);
    wmShape.Width = WordApp.InchesToPoints((float)8.3);
    wmShape.WrapFormat.AllowOverlap = -1;
    wmShape.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
    wmShape.WrapFormat.Type = WdWrapType.wdWrapBehind;
    wmShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
    wmShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
    wmShape.Left = (float)WdShapePosition.wdShapeCenter;
    wmShape.Top = (float)WdShapePosition.wdShapeCenter;
}
WordApp.Visible = true;

最新更新