Word文档多种背景

发布于 2025-01-05 17:48:28 字数 1585 浏览 1 评论 0原文

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

我这样尝试:

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 个水印。

有什么想法吗?

I have a file "test.docx" with 3 page sections, and each section is unlinked from the previous page. Now I want to be able to set a different background/watermark for each section. I just don't know how to select the other 2 sections (or.. not the first one).

I tried it like this:

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;

but it just drops all 3 watermarks on the first section.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2025-01-12 17:48:29

WtFudgeE

如果将所有水印放在同一位置,则可能存在这些部分链接的可能性。一种检查方法是打开 Word 文档并执行以下操作:

打开页眉和页脚选项卡,然后在选项中,转到页眉和布局并选择“不同的首页”。

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

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

WtFudgE

If it is dropping all water marks in same place then there could be a possibility that the sections are linked. One way to check would be to open the Word document and do the following

Open the Headers and Footers Tabs and in the Options, go to the Headers and Layouts and select "Different First Page"

Once you have done that now test your code. :)

What MS Word Version are you using? Maybe, I can post a relevant snapshot?

她比我温柔 2025-01-12 17:48:29

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

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

I actually found a work around, after 3 days of searching and discussing. Here's my solution:

//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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文