Копировать заголовок Word из одного документа в другой, сохраняя исходное форматирование - PullRequest
0 голосов
/ 25 октября 2019

Я пытаюсь написать инструмент для массового копирования заголовка из одного документа Word в несколько других. Я использовал код от Microsoft ниже для замены заголовка Word.

https://docs.microsoft.com/en-us/office/open-xml/how-to-replace-the-header-in-a-word-processing-document

Однако, это будет копировать только содержимое, а также любое встроенное форматированиев самом заголовке. Он не копирует форматирование из стилей, указанных в заголовке. Мне нужна функция копирования, аналогичная опции вставки в Word, которая называется «Сохранить исходное форматирование».

Я пытался использовать инструмент повышения производительности Open XML SDK 2.5, чтобы отразить код из существующего файла, но мне трудно даже определить, на какой стиль ссылаются. Приведенный ниже код представляет заголовок документа Word, в котором содержимое отображается шрифтом размера 11, но я не могу понять, на что ссылается шрифт размера 11. Я попытался найти в основном коде документа ссылки на различные 6-значные шестнадцатеричные коды, найденные в коде заголовка, но ничего не нашел.

// Generates content of part.
private void GeneratePartContent(HeaderPart part)
{
    Header header1 = new Header(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 w16se w16cid wp14" }  };
    header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
    header1.AddNamespaceDeclaration("cx", "http://schemas.microsoft.com/office/drawing/2014/chartex");
    header1.AddNamespaceDeclaration("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex");
    header1.AddNamespaceDeclaration("cx2", "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex");
    header1.AddNamespaceDeclaration("cx3", "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex");
    header1.AddNamespaceDeclaration("cx4", "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex");
    header1.AddNamespaceDeclaration("cx5", "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex");
    header1.AddNamespaceDeclaration("cx6", "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex");
    header1.AddNamespaceDeclaration("cx7", "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex");
    header1.AddNamespaceDeclaration("cx8", "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex");
    header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
    header1.AddNamespaceDeclaration("aink", "http://schemas.microsoft.com/office/drawing/2016/ink");
    header1.AddNamespaceDeclaration("am3d", "http://schemas.microsoft.com/office/drawing/2017/model3d");
    header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
    header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
    header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
    header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
    header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
    header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
    header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
    header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
    header1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml");
    header1.AddNamespaceDeclaration("w16cid", "http://schemas.microsoft.com/office/word/2016/wordml/cid");
    header1.AddNamespaceDeclaration("w16se", "http://schemas.microsoft.com/office/word/2015/wordml/symex");
    header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
    header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
    header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
    header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

    Paragraph paragraph1 = new Paragraph(){ RsidParagraphMarkRevision = "00FB30A2", RsidParagraphAddition = "00B60876", RsidParagraphProperties = "00B60876", RsidRunAdditionDefault = "00D61F9A", ParagraphId = "5961FCE8", TextId = "5CEF3C23" };

    ParagraphProperties paragraphProperties1 = new ParagraphProperties();
    WidowControl widowControl1 = new WidowControl(){ Val = false };

    Tabs tabs1 = new Tabs();
    TabStop tabStop1 = new TabStop(){ Val = TabStopValues.Center, Position = 4680 };
    TabStop tabStop2 = new TabStop(){ Val = TabStopValues.Right, Position = 9360 };

    tabs1.Append(tabStop1);
    tabs1.Append(tabStop2);
    SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines(){ After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

    ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
    RunFonts runFonts1 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    paragraphMarkRunProperties1.Append(runFonts1);

    paragraphProperties1.Append(widowControl1);
    paragraphProperties1.Append(tabs1);
    paragraphProperties1.Append(spacingBetweenLines1);
    paragraphProperties1.Append(paragraphMarkRunProperties1);

    Run run1 = new Run();

    RunProperties runProperties1 = new RunProperties();
    RunFonts runFonts2 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties1.Append(runFonts2);
    Text text1 = new Text();
    text1.Text = "Test";

    run1.Append(runProperties1);
    run1.Append(text1);

    Run run2 = new Run(){ RsidRunProperties = "00FB30A2", RsidRunAddition = "00B60876" };

    RunProperties runProperties2 = new RunProperties();
    RunFonts runFonts3 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties2.Append(runFonts3);
    Text text2 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
    text2.Text = " ";

    run2.Append(runProperties2);
    run2.Append(text2);

    Run run3 = new Run();

    RunProperties runProperties3 = new RunProperties();
    RunFonts runFonts4 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties3.Append(runFonts4);
    Text text3 = new Text();
    text3.Text = "Test";

    run3.Append(runProperties3);
    run3.Append(text3);

    Run run4 = new Run(){ RsidRunProperties = "00FB30A2", RsidRunAddition = "00B60876" };

    RunProperties runProperties4 = new RunProperties();
    RunFonts runFonts5 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties4.Append(runFonts5);
    Text text4 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
    text4.Text = " #0000";

    run4.Append(runProperties4);
    run4.Append(text4);
    BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
    BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };

    Run run5 = new Run(){ RsidRunProperties = "00FB30A2", RsidRunAddition = "00B60876" };

    RunProperties runProperties5 = new RunProperties();
    RunFonts runFonts6 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties5.Append(runFonts6);
    TabChar tabChar1 = new TabChar();

    run5.Append(runProperties5);
    run5.Append(tabChar1);

    Run run6 = new Run(){ RsidRunProperties = "00FB30A2", RsidRunAddition = "00B60876" };

    RunProperties runProperties6 = new RunProperties();
    RunFonts runFonts7 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties6.Append(runFonts7);
    TabChar tabChar2 = new TabChar();
    Text text5 = new Text();
    text5.Text = "Address";

    run6.Append(runProperties6);
    run6.Append(tabChar2);
    run6.Append(text5);

    paragraph1.Append(paragraphProperties1);
    paragraph1.Append(run1);
    paragraph1.Append(run2);
    paragraph1.Append(run3);
    paragraph1.Append(run4);
    paragraph1.Append(bookmarkStart1);
    paragraph1.Append(bookmarkEnd1);
    paragraph1.Append(run5);
    paragraph1.Append(run6);

    Paragraph paragraph2 = new Paragraph(){ RsidParagraphMarkRevision = "00FB30A2", RsidParagraphAddition = "00B60876", RsidParagraphProperties = "00B60876", RsidRunAdditionDefault = "00B60876", ParagraphId = "1F303E6E", TextId = "77777777" };

    ParagraphProperties paragraphProperties2 = new ParagraphProperties();
    WidowControl widowControl2 = new WidowControl(){ Val = false };

    Tabs tabs2 = new Tabs();
    TabStop tabStop3 = new TabStop(){ Val = TabStopValues.Center, Position = 4680 };
    TabStop tabStop4 = new TabStop(){ Val = TabStopValues.Right, Position = 9360 };

    tabs2.Append(tabStop3);
    tabs2.Append(tabStop4);
    SpacingBetweenLines spacingBetweenLines2 = new SpacingBetweenLines(){ After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

    ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties();
    RunFonts runFonts8 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    paragraphMarkRunProperties2.Append(runFonts8);

    paragraphProperties2.Append(widowControl2);
    paragraphProperties2.Append(tabs2);
    paragraphProperties2.Append(spacingBetweenLines2);
    paragraphProperties2.Append(paragraphMarkRunProperties2);

    Run run7 = new Run(){ RsidRunProperties = "00FB30A2" };

    RunProperties runProperties7 = new RunProperties();
    RunFonts runFonts9 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties7.Append(runFonts9);
    Text text6 = new Text();
    text6.Text = "October 24, 2019";

    run7.Append(runProperties7);
    run7.Append(text6);

    Run run8 = new Run(){ RsidRunProperties = "00FB30A2" };

    RunProperties runProperties8 = new RunProperties();
    RunFonts runFonts10 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties8.Append(runFonts10);
    TabChar tabChar3 = new TabChar();

    run8.Append(runProperties8);
    run8.Append(tabChar3);

    Run run9 = new Run(){ RsidRunProperties = "00FB30A2" };

    RunProperties runProperties9 = new RunProperties();
    RunFonts runFonts11 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "Calibri" };

    runProperties9.Append(runFonts11);
    TabChar tabChar4 = new TabChar();
    Text text7 = new Text();
    text7.Text = "Anytown, ST 00000";

    run9.Append(runProperties9);
    run9.Append(tabChar4);
    run9.Append(text7);

    paragraph2.Append(paragraphProperties2);
    paragraph2.Append(run7);
    paragraph2.Append(run8);
    paragraph2.Append(run9);

    Paragraph paragraph3 = new Paragraph(){ RsidParagraphAddition = "00BA5AD4", RsidRunAdditionDefault = "00BA5AD4", ParagraphId = "12CE7885", TextId = "77777777" };

    ParagraphProperties paragraphProperties3 = new ParagraphProperties();
    WidowControl widowControl3 = new WidowControl(){ Val = false };
    AutoSpaceDE autoSpaceDE1 = new AutoSpaceDE(){ Val = false };
    AutoSpaceDN autoSpaceDN1 = new AutoSpaceDN(){ Val = false };
    AdjustRightIndent adjustRightIndent1 = new AdjustRightIndent(){ Val = false };
    SpacingBetweenLines spacingBetweenLines3 = new SpacingBetweenLines(){ After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

    ParagraphMarkRunProperties paragraphMarkRunProperties3 = new ParagraphMarkRunProperties();
    RunFonts runFonts12 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman" };
    FontSize fontSize1 = new FontSize(){ Val = "10" };
    FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript(){ Val = "10" };

    paragraphMarkRunProperties3.Append(runFonts12);
    paragraphMarkRunProperties3.Append(fontSize1);
    paragraphMarkRunProperties3.Append(fontSizeComplexScript1);

    paragraphProperties3.Append(widowControl3);
    paragraphProperties3.Append(autoSpaceDE1);
    paragraphProperties3.Append(autoSpaceDN1);
    paragraphProperties3.Append(adjustRightIndent1);
    paragraphProperties3.Append(spacingBetweenLines3);
    paragraphProperties3.Append(paragraphMarkRunProperties3);

    paragraph3.Append(paragraphProperties3);

    header1.Append(paragraph1);
    header1.Append(paragraph2);
    header1.Append(paragraph3);

    part.Header = header1;
}

Основываясь на комментариях Синди, япросмотрел коллекцию Styles для документа, и я нашел то, что, я предполагаю, является стилем, на который ссылаются, но я не уверен, где на него ссылаются, или это просто какой-то встроенный стиль, который Word всегда использует дляheader.

// Creates an Style instance and adds its children.
public Style GenerateStyle()
{
    Style style1 = new Style(){ Type = StyleValues.Character, StyleId = "HeaderChar", CustomStyle = true };
    StyleName styleName1 = new StyleName(){ Val = "Header Char" };
    LinkedStyle linkedStyle1 = new LinkedStyle(){ Val = "Header" };
    UIPriority uIPriority1 = new UIPriority(){ Val = 99 };
    Rsid rsid1 = new Rsid(){ Val = "00386EFA" };

    StyleRunProperties styleRunProperties1 = new StyleRunProperties();
    FontSize fontSize1 = new FontSize(){ Val = "22" };
    FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript(){ Val = "22" };

    styleRunProperties1.Append(fontSize1);
    styleRunProperties1.Append(fontSizeComplexScript1);

    style1.Append(styleName1);
    style1.Append(linkedStyle1);
    style1.Append(uIPriority1);
    style1.Append(rsid1);
    style1.Append(styleRunProperties1);
    return style1;
}

Как я могу сделать это с помощью Open XML SDK?

...