处理MS Word中的特定页面 - XML文件 - (使用C#序列化)

时间:2013-02-09 10:46:42

标签: c# ms-word xml-serialization linq-to-xml

我正在处理 XML文件,这是 word文档

在该文件中,我试图插入一些图像并进行一些更改。

为了做到这一点,我需要能够在特定页面(word文档的页面)中进行更改。那是因为我想在每个页面上添加一个图像(在我的word文档中)。

问题是 - 根据我的理解,Word XML文件主要基于段落。并且..我不知道哪个段落与哪个单词页面相关联!

这是用于演示问题的代码 - 这是一个典型的带有段落的XML Word文档代码。在代码的开头,您可以看到我在第一段中插入的图像。结果我在word文档的第一页上得到了一个图像。但我想以编程方式为每个单词页面插入该图像。问题是我不知道选择哪个段落,因为我无法分辨哪个段落与哪个页面相关...

        <w:rPr>
      <w:rFonts w:hint="cs" />
      <w:rtl />
    </w:rPr>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:rFonts w:hint="cs" />
      <w:noProof />
      <w:rtl />
    </w:rPr>
    <w:drawing>
      <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251658240" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1">
        <wp:simplePos x="0" y="0" />
        <wp:positionH relativeFrom="column">
          <wp:posOffset>-1143000</wp:posOffset>
        </wp:positionH>
        <wp:positionV relativeFrom="paragraph">
          <wp:posOffset>-914400</wp:posOffset>
        </wp:positionV>
        <wp:extent cx="7560309" cy="10692130" />
        <wp:effectExtent l="0" t="0" r="3175" b="0" />
        <wp:wrapNone />
        <wp:docPr id="1" name="תמונה 1" />
        <wp:cNvGraphicFramePr>
          <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
        </wp:cNvGraphicFramePr>
        <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
          <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
            <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
              <pic:nvPicPr>
                <pic:cNvPr id="0" name="" />
                <pic:cNvPicPr />
              </pic:nvPicPr>
              <pic:blipFill>
                <a:blip r:embed="rId8">
                  <a:extLst>
                    <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                      <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
                    </a:ext>
                  </a:extLst>
                </a:blip>
                <a:stretch>
                  <a:fillRect />
                </a:stretch>
              </pic:blipFill>
              <pic:spPr>
                <a:xfrm>
                  <a:off x="0" y="0" />
                  <a:ext cx="7560309" cy="10692130" />
                </a:xfrm>
                <a:prstGeom prst="rect">
                  <a:avLst />
                </a:prstGeom>
              </pic:spPr>
            </pic:pic>
          </a:graphicData>
        </a:graphic>
        <wp14:sizeRelH relativeFrom="margin">
          <wp14:pctWidth>0</wp14:pctWidth>
        </wp14:sizeRelH>
        <wp14:sizeRelV relativeFrom="margin">
          <wp14:pctHeight>0</wp14:pctHeight>
        </wp14:sizeRelV>
      </wp:anchor>
    </w:drawing>
  </w:r>
</w:p>
<w:p w:rsidR="00DE086D" w:rsidRDefault="00DE086D">
  <w:pPr>
    <w:rPr>
      <w:rFonts w:hint="cs" />
      <w:rtl />
    </w:rPr>
  </w:pPr>
</w:p>
<w:p w:rsidR="00DE086D" w:rsidRDefault="00DE086D">
  <w:pPr>
    <w:rPr>
      <w:rFonts w:hint="cs" />
      <w:rtl />
    </w:rPr>
  </w:pPr>
</w:p>
<w:p w:rsidR="00DE086D" w:rsidRDefault="00DE086D">
  <w:pPr>
    <w:rPr>
      <w:rFonts w:hint="cs" />
      <w:rtl />
    </w:rPr>
  </w:pPr>
</w:p>

以下是生成该文档的代码(我正在使用Open XML SDK 2.0 Microsoft Office生产力工具为我创建代码)

        using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    using Wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using A = DocumentFormat.OpenXml.Drawing;
    using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
    using A14 = DocumentFormat.OpenXml.Office2010.Drawing;
    using Wp14 = DocumentFormat.OpenXml.Office2010.Word.Drawing;

    namespace GeneratedCode
        {
            public class GeneratedClass
            {
                // Creates an Document instance and adds its children.
                public Document GenerateDocument()
                {
        Document document1 = new Document(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 wp14" }  };
        document1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
        document1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
        document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
        document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
        document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
        document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
        document1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
        document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
        document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
        document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        document1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
        document1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
        document1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
        document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
        document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

        Body body1 = new Body();

        Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00C85507", RsidRunAdditionDefault = "00930644" };

        ParagraphProperties paragraphProperties1 = new ParagraphProperties();

        ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
        RunFonts runFonts1 = new RunFonts(){ Hint = FontTypeHintValues.ComplexScript };
        RightToLeftText rightToLeftText1 = new RightToLeftText();

        paragraphMarkRunProperties1.Append(runFonts1);
        paragraphMarkRunProperties1.Append(rightToLeftText1);

        paragraphProperties1.Append(paragraphMarkRunProperties1);

        Run run1 = new Run();

        RunProperties runProperties1 = new RunProperties();
        RunFonts runFonts2 = new RunFonts(){ Hint = FontTypeHintValues.ComplexScript };
        NoProof noProof1 = new NoProof();
        RightToLeftText rightToLeftText2 = new RightToLeftText();

        runProperties1.Append(runFonts2);
        runProperties1.Append(noProof1);
        runProperties1.Append(rightToLeftText2);

        Drawing drawing1 = new Drawing();

        Wp.Anchor anchor1 = new Wp.Anchor(){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)114300U, DistanceFromRight = (UInt32Value)114300U, SimplePos = false, RelativeHeight = (UInt32Value)251658240U, BehindDoc = true, Locked = false, LayoutInCell = true, AllowOverlap = true };
        Wp.SimplePosition simplePosition1 = new Wp.SimplePosition(){ X = 0L, Y = 0L };

        Wp.HorizontalPosition horizontalPosition1 = new Wp.HorizontalPosition(){ RelativeFrom = Wp.HorizontalRelativePositionValues.Column };
        Wp.PositionOffset positionOffset1 = new Wp.PositionOffset();
        positionOffset1.Text = "-1143000";

        horizontalPosition1.Append(positionOffset1);

        Wp.VerticalPosition verticalPosition1 = new Wp.VerticalPosition(){ RelativeFrom = Wp.VerticalRelativePositionValues.Paragraph };
        Wp.PositionOffset positionOffset2 = new Wp.PositionOffset();
        positionOffset2.Text = "-914400";

        verticalPosition1.Append(positionOffset2);
        Wp.Extent extent1 = new Wp.Extent(){ Cx = 7560309L, Cy = 10692130L };
        Wp.EffectExtent effectExtent1 = new Wp.EffectExtent(){ LeftEdge = 0L, TopEdge = 0L, RightEdge = 3175L, BottomEdge = 0L };
        Wp.WrapNone wrapNone1 = new Wp.WrapNone();
        Wp.DocProperties docProperties1 = new Wp.DocProperties(){ Id = (UInt32Value)1U, Name = "תמונה 1" };

        Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();

        A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks(){ NoChangeAspect = true };
        graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

        nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);

        A.Graphic graphic1 = new A.Graphic();
        graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

        A.GraphicData graphicData1 = new A.GraphicData(){ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };

        Pic.Picture picture1 = new Pic.Picture();
        picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

        Pic.NonVisualPictureProperties nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
        Pic.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties(){ Id = (UInt32Value)0U, Name = "" };
        Pic.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();

        nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
        nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);

        Pic.BlipFill blipFill1 = new Pic.BlipFill();

        A.Blip blip1 = new A.Blip(){ Embed = "rId8" };

        A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();

        A.BlipExtension blipExtension1 = new A.BlipExtension(){ Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };

        A14.UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi(){ Val = false };
        useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");

        blipExtension1.Append(useLocalDpi1);

        blipExtensionList1.Append(blipExtension1);

        blip1.Append(blipExtensionList1);

        A.Stretch stretch1 = new A.Stretch();
        A.FillRectangle fillRectangle1 = new A.FillRectangle();

        stretch1.Append(fillRectangle1);

        blipFill1.Append(blip1);
        blipFill1.Append(stretch1);

        Pic.ShapeProperties shapeProperties1 = new Pic.ShapeProperties();

        A.Transform2D transform2D1 = new A.Transform2D();
        A.Offset offset1 = new A.Offset(){ X = 0L, Y = 0L };
        A.Extents extents1 = new A.Extents(){ Cx = 7560309L, Cy = 10692130L };

        transform2D1.Append(offset1);
        transform2D1.Append(extents1);

        A.PresetGeometry presetGeometry1 = new A.PresetGeometry(){ Preset = A.ShapeTypeValues.Rectangle };
        A.AdjustValueList adjustValueList1 = new A.AdjustValueList();

        presetGeometry1.Append(adjustValueList1);

        shapeProperties1.Append(transform2D1);
        shapeProperties1.Append(presetGeometry1);

        picture1.Append(nonVisualPictureProperties1);
        picture1.Append(blipFill1);
        picture1.Append(shapeProperties1);

        graphicData1.Append(picture1);

        graphic1.Append(graphicData1);

        Wp14.RelativeWidth relativeWidth1 = new Wp14.RelativeWidth(){ ObjectId = Wp14.SizeRelativeHorizontallyValues.Margin };
        Wp14.PercentageWidth percentageWidth1 = new Wp14.PercentageWidth();
        percentageWidth1.Text = "0";

        relativeWidth1.Append(percentageWidth1);

        Wp14.RelativeHeight relativeHeight1 = new Wp14.RelativeHeight(){ RelativeFrom = Wp14.SizeRelativeVerticallyValues.Margin };
        Wp14.PercentageHeight percentageHeight1 = new Wp14.PercentageHeight();
        percentageHeight1.Text = "0";

        relativeHeight1.Append(percentageHeight1);

        anchor1.Append(simplePosition1);
        anchor1.Append(horizontalPosition1);
        anchor1.Append(verticalPosition1);
        anchor1.Append(extent1);
        anchor1.Append(effectExtent1);
        anchor1.Append(wrapNone1);
        anchor1.Append(docProperties1);
        anchor1.Append(nonVisualGraphicFrameDrawingProperties1);
        anchor1.Append(graphic1);
        anchor1.Append(relativeWidth1);
        anchor1.Append(relativeHeight1);

        drawing1.Append(anchor1);

        run1.Append(runProperties1);
        run1.Append(drawing1);

        paragraph1.Append(paragraphProperties1);
        paragraph1.Append(run1);

        Paragraph paragraph2 = new Paragraph(){ RsidParagraphAddition = "00DE086D", RsidRunAdditionDefault = "00DE086D" };

        ParagraphProperties paragraphProperties2 = new ParagraphProperties();


        Run run1 = new Run();

        RunProperties runProperties1 = new RunProperties();
        RunFonts runFonts2 = new RunFonts(){ Hint = FontTypeHintValues.ComplexScript };
        NoProof noProof1 = new NoProof();
        RightToLeftText rightToLeftText2 = new RightToLeftText();

        runProperties1.Append(runFonts2);
        runProperties1.Append(noProof1);
        runProperties1.Append(rightToLeftText2);

        Drawing drawing1 = new Drawing();

        Wp.Anchor anchor1 = new Wp.Anchor(){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)114300U, DistanceFromRight = (UInt32Value)114300U, SimplePos = false, RelativeHeight = (UInt32Value)251658240U, BehindDoc = true, Locked = false, LayoutInCell = true, AllowOverlap = true };
        Wp.SimplePosition simplePosition1 = new Wp.SimplePosition(){ X = 0L, Y = 0L };

        Wp.HorizontalPosition horizontalPosition1 = new Wp.HorizontalPosition(){ RelativeFrom = Wp.HorizontalRelativePositionValues.Column };
        Wp.PositionOffset positionOffset1 = new Wp.PositionOffset();
        positionOffset1.Text = "-1143000";

        horizontalPosition1.Append(positionOffset1);

        Wp.VerticalPosition verticalPosition1 = new Wp.VerticalPosition(){ RelativeFrom = Wp.VerticalRelativePositionValues.Paragraph };
        Wp.PositionOffset positionOffset2 = new Wp.PositionOffset();
        positionOffset2.Text = "-914400";

        verticalPosition1.Append(positionOffset2);
        Wp.Extent extent1 = new Wp.Extent(){ Cx = 7560309L, Cy = 10692130L };
        Wp.EffectExtent effectExtent1 = new Wp.EffectExtent(){ LeftEdge = 0L, TopEdge = 0L, RightEdge = 3175L, BottomEdge = 0L };
        Wp.WrapNone wrapNone1 = new Wp.WrapNone();
        Wp.DocProperties docProperties1 = new Wp.DocProperties(){ Id = (UInt32Value)1U, Name = "תמונה 1" };

        Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();

        A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks(){ NoChangeAspect = true };
        graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

        nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);

        A.Graphic graphic1 = new A.Graphic();
        graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

        A.GraphicData graphicData1 = new A.GraphicData(){ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };

        Pic.Picture picture1 = new Pic.Picture();
        picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

        Pic.NonVisualPictureProperties nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
        Pic.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties(){ Id = (UInt32Value)0U, Name = "" };
        Pic.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();

        nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
        nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);

        Pic.BlipFill blipFill1 = new Pic.BlipFill();

        A.Blip blip1 = new A.Blip(){ Embed = "rId8" };

        A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();

        SectionProperties sectionProperties1 = new SectionProperties(){ RsidR = "00DE086D", RsidSect = "00C85507" };
        HeaderReference headerReference1 = new HeaderReference(){ Type = HeaderFooterValues.Even, Id = "rId9" };
        HeaderReference headerReference2 = new HeaderReference(){ Type = HeaderFooterValues.Default, Id = "rId10" };
        FooterReference footerReference1 = new FooterReference(){ Type = HeaderFooterValues.Even, Id = "rId11" };
        FooterReference footerReference2 = new FooterReference(){ Type = HeaderFooterValues.Default, Id = "rId12" };
        HeaderReference headerReference3 = new HeaderReference(){ Type = HeaderFooterValues.First, Id = "rId13" };
        FooterReference footerReference3 = new FooterReference(){ Type = HeaderFooterValues.First, Id = "rId14" };
        PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)11906U, Height = (UInt32Value)16838U };
        PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)708U, Footer = (UInt32Value)708U, Gutter = (UInt32Value)0U };
        Columns columns1 = new Columns(){ Space = "708" };
        BiDi biDi1 = new BiDi();
        GutterOnRight gutterOnRight1 = new GutterOnRight();
        DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };

        sectionProperties1.Append(headerReference1);
        sectionProperties1.Append(headerReference2);
        sectionProperties1.Append(footerReference1);
        sectionProperties1.Append(footerReference2);
        sectionProperties1.Append(headerReference3);
        sectionProperties1.Append(footerReference3);
        sectionProperties1.Append(pageSize1);
        sectionProperties1.Append(pageMargin1);
        sectionProperties1.Append(columns1);
        sectionProperties1.Append(biDi1);
        sectionProperties1.Append(gutterOnRight1);
        sectionProperties1.Append(docGrid1);

        body1.Append(paragraph1);
        body1.Append(paragraph2);
        body1.Append(paragraph3);
        body1.Append(paragraph4);
        body1.Append(paragraph5);
        body1.Append(paragraph6);
        body1.Append(paragraph7);
        body1.Append(paragraph8);
        body1.Append(paragraph9);
        body1.Append(paragraph10);
        body1.Append(paragraph11);
        body1.Append(paragraph12);
        body1.Append(paragraph13);
        body1.Append(paragraph14);
        body1.Append(paragraph15);
        body1.Append(paragraph16);
        body1.Append(paragraph17);
        body1.Append(paragraph18);
        body1.Append(paragraph19);
        body1.Append(paragraph20);
        body1.Append(paragraph21);
        body1.Append(paragraph22);
        body1.Append(paragraph23);
        body1.Append(paragraph24);
        body1.Append(paragraph25);
        body1.Append(paragraph26);
        body1.Append(paragraph27);
        body1.Append(paragraph28);
        body1.Append(paragraph29);
        body1.Append(paragraph30);
        body1.Append(paragraph31);
        body1.Append(paragraph32);
        body1.Append(paragraph33);

        body1.Append(sectionProperties1);

        document1.Append(body1);
        return document1;
                }


            }
        }

我是XML的新手,所以我尝试搜索它为我提供的功能。我正在使用C#来序列化它。我发现C#为你提供了一些处理Word文档的库,但我担心它们会损害我程序的性能。

我还听说过一些名为LINQ的东西,它为你提供了一些XML文件的功能,但是我找不到任何与页面或行相关的东西。

我的问题是 - 使用C#将图像插入我的word文档(XML文件)的所有页面的最佳效果(在性能=时间方面)

非常感谢!

Lior

0 个答案:

没有答案