我有一个包含3个页面部分的文件“test.docx”,每个部分都与上一页断开链接。现在我希望能够为每个部分设置不同的背景/水印。我只是不知道如何选择其他2个部分(或者......而不是第一部分)。
我试过这样:
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个水印。
有什么想法吗?
答案 0 :(得分:0)
WtFudgE
如果它在同一个地方掉落所有水印,那么这些部分可能会被连接起来。检查的一种方法是打开Word文档并执行以下操作
打开页眉和页脚选项卡,在选项中,转到页眉和布局,然后选择“不同的第一页”
完成后,现在测试您的代码。 :)
您使用的是什么MS Word版本?也许,我可以发布相关快照吗?
答案 1 :(得分:0)
//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:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg");
images.Add(@"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.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;