Пример #1
0
        [InlineData("RaTest07.docx", "/w:document/w:body/w:p[2]", @"TOA \h \c ""1"" \p")]      // Inserts a table of figures
        public void Sample(string fileName, string xPath, string switches)
        {
            var srcFile = new FileInfo(Path.Combine("../../../Word/Samples/ReferenceAdder/", fileName));
            var file    = Path.Combine(TempDir, srcFile.Name);

            srcFile.CopyTo(file, true);

            using var wDoc = WordprocessingDocument.Open(file, true);
            ReferenceAdder.AddToc(wDoc, xPath, switches, null, null);
        }
Пример #2
0
 protected override void ProcessRecord()
 {
     foreach (var document in AllDocuments("Add-OpenXmlDocumentTOA"))
     {
         try
         {
             if (!(document is WmlDocument))
             {
                 throw new PowerToolsDocumentException("Not a wordprocessing document.");
             }
             OutputDocument(ReferenceAdder.AddToa((WmlDocument)document, InsertionPoint, Switches, null));
         }
         catch (Exception e)
         {
             WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
         }
     }
 }
Пример #3
0
        public void DB009_ShredDocument()
        {
            var spec = new FileInfo(Path.Combine(_sourceDir.FullName, "DB007-Spec.docx"));
            // Shred a document into multiple parts for each section
            List <DocumentInfo> documentList;

            using (var doc = WordprocessingDocument.Open(spec.FullName, false))
            {
                var sectionCounts = doc
                                    .MainDocumentPart
                                    .GetXDocument()
                                    .Root
                                    .Element(W.body)
                                    .Elements()
                                    .Rollup(0, (pi, last) => (string)pi
                                            .Elements(W.pPr)
                                            .Elements(W.pStyle)
                                            .Attributes(W.val)
                                            .FirstOrDefault() == "Heading1" ? last + 1 : last);
                var beforeZipped = doc
                                   .MainDocumentPart
                                   .GetXDocument()
                                   .Root
                                   .Element(W.body)
                                   .Elements()
                                   .Select((p, i) => new
                {
                    Paragraph = p,
                    Index     = i,
                });
                var zipped = PtExtensions.PtZip(beforeZipped, sectionCounts, (pi, sc) => new
                {
                    Paragraph    = pi.Paragraph,
                    Index        = pi.Index,
                    SectionIndex = sc,
                });
                documentList = zipped
                               .GroupAdjacent(p => p.SectionIndex)
                               .Select(g => new DocumentInfo
                {
                    DocumentNumber = g.Key,
                    Start          = g.First().Index,
                    Count          = g.Last().Index - g.First().Index + 1,
                })
                               .ToList();
            }
            foreach (var doc in documentList)
            {
                var fileName       = $"DB009-Section{doc.DocumentNumber:000}.docx";
                var fiSection      = new FileInfo(Path.Combine(TempDir, fileName));
                var documentSource = new List <ISource> {
                    new Source(new WmlDocument(spec.FullName), doc.Start, doc.Count, true)
                };
                DocumentBuilder.BuildDocument(documentSource, fiSection.FullName);
                Validate(fiSection);
            }

            // Re-assemble the parts into a single document.
            var sources = new DirectoryInfo(TempDir)
                          .GetFiles("DB009-Section*.docx")
                          .Select(d => (ISource) new Source(new WmlDocument(d.FullName), true))
                          .ToList();
            var fiReassembled = new FileInfo(Path.Combine(TempDir, "DB009-Reassembled.docx"));

            DocumentBuilder.BuildDocument(sources, fiReassembled.FullName);
            using (var doc = WordprocessingDocument.Open(fiReassembled.FullName, true))
            {
                ReferenceAdder.AddToc(doc, "/w:document/w:body/w:p[1]",
                                      @"TOC \o '1-3' \h \z \u", null, null);
            }
            Validate(fiReassembled);
        }
    private static void Main()
    {
        var n      = DateTime.Now;
        var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));

        tempDi.Create();

        var di2 = new DirectoryInfo("../../");

        foreach (var file in di2.GetFiles("*.docx"))
        {
            file.CopyTo(Path.Combine(tempDi.FullName, file.Name));
        }

        var filesToProcess = new List <string>();

        // Inserts a basic TOC before the first paragraph of the document
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test01.docx"), true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }

        // Inserts a TOC after the title of the document
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test02.docx"), true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[2]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }

        // Inserts a TOC with a different title
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test03.docx"), true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", "Table of Contents", null);
        }

        // Inserts a TOC that includes headings through level 4
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test04.docx"), true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-4' \h \z \u", null, null);
        }

        // Inserts a table of figures
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test05.docx"), true))
        {
            ReferenceAdder.AddTof(wdoc, "/w:document/w:body/w:p[2]",
                                  @"TOC \h \z \c ""Figure""", null);
        }

        // Inserts a basic TOC before the first paragraph of the document.
        // Test06.docx does not include a StylesWithEffects part.
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test06.docx"), true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }

        // Inserts a TOA before the first paragraph of the document.
        using (var wdoc =
                   WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test07.docx"), true))
        {
            ReferenceAdder.AddToa(wdoc, "/w:document/w:body/w:p[2]",
                                  @"TOA \h \c ""1"" \p", null);
        }
    }
    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo(".");

        foreach (var file in di.GetFiles("*.docx"))
        {
            file.Delete();
        }
        DirectoryInfo di2 = new DirectoryInfo("../../");

        foreach (var file in di2.GetFiles("*.docx"))
        {
            file.CopyTo(di.FullName + "/" + file.Name);
        }

        List <string> filesToProcess = new List <string>();

        // Inserts a basic TOC before the first paragraph of the document
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test01.docx", true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }

        // Inserts a TOC after the title of the document
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test02.docx", true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[2]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }

        // Inserts a TOC with a different title
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test03.docx", true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", "Table of Contents", null);
        }

        // Inserts a TOC that includes headings through level 4
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test04.docx", true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-4' \h \z \u", null, null);
        }

        // Inserts a table of figures
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test05.docx", true))
        {
            ReferenceAdder.AddTof(wdoc, "/w:document/w:body/w:p[2]",
                                  @"TOC \h \z \c ""Figure""", null);
        }

        // Inserts a basic TOC before the first paragraph of the document.
        // Test06.docx does not include a StylesWithEffects part.
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test06.docx", true))
        {
            ReferenceAdder.AddToc(wdoc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }

        // Inserts a TOA before the first paragraph of the document.
        using (WordprocessingDocument wdoc =
                   WordprocessingDocument.Open("Test07.docx", true))
        {
            ReferenceAdder.AddToa(wdoc, "/w:document/w:body/w:p[2]",
                                  @"TOA \h \c ""1"" \p", null);
        }
    }
    static void Main(string[] args)
    {
        // Insert an abstract and author biography into a white paper.
        List <Source> sources = null;

#if WhitePaper
        sources = new List <Source>()
        {
            new Source(new WmlDocument("../../WhitePaper.docx"), 0, 1, true),
            new Source(new WmlDocument("../../Abstract.docx"), false),
            new Source(new WmlDocument("../../AuthorBiography.docx"), false),
            new Source(new WmlDocument("../../WhitePaper.docx"), 1, false),
        };
        DocumentBuilder.BuildDocument(sources, "AssembledPaper.docx");
#endif

#if DeleteNotes
        // Delete all paragraphs with a specific style.
        using (WordprocessingDocument doc =
                   WordprocessingDocument.Open("../../Notes.docx", false))
        {
            sources = doc
                      .MainDocumentPart
                      .GetXDocument()
                      .Root
                      .Element(W.body)
                      .Elements()
                      .Select((p, i) => new
            {
                Paragraph = p,
                Index     = i,
            })
                      .GroupAdjacent(pi => (string)pi.Paragraph
                                     .Elements(W.pPr)
                                     .Elements(W.pStyle)
                                     .Attributes(W.val)
                                     .FirstOrDefault() != "Note")
                      .Where(g => g.Key == true)
                      .Select(g => new Source(
                                  new WmlDocument("../../Notes.docx"), g.First().Index,
                                  g.Last().Index - g.First().Index + 1, true))
                      .ToList();
        }
        DocumentBuilder.BuildDocument(sources, "NewNotes.docx");
#endif

#if Shred
        // Shred a document into multiple parts for each section
        List <DocumentInfo> documentList;
        using (WordprocessingDocument doc =
                   WordprocessingDocument.Open("../../Spec.docx", false))
        {
            var sectionCounts = doc
                                .MainDocumentPart
                                .GetXDocument()
                                .Root
                                .Element(W.body)
                                .Elements()
                                .Rollup(0, (pi, last) => (string)pi
                                        .Elements(W.pPr)
                                        .Elements(W.pStyle)
                                        .Attributes(W.val)
                                        .FirstOrDefault() == "Heading1" ? last + 1 : last);
            var beforeZipped = doc
                               .MainDocumentPart
                               .GetXDocument()
                               .Root
                               .Element(W.body)
                               .Elements()
                               .Select((p, i) => new
            {
                Paragraph = p,
                Index     = i,
            });
            var zipped = PtExtensions.PtZip(beforeZipped, sectionCounts, (pi, sc) => new
            {
                Paragraph    = pi.Paragraph,
                Index        = pi.Index,
                SectionIndex = sc,
            });
            documentList = zipped
                           .GroupAdjacent(p => p.SectionIndex)
                           .Select(g => new DocumentInfo
            {
                DocumentNumber = g.Key,
                Start          = g.First().Index,
                Count          = g.Last().Index - g.First().Index + 1,
            })
                           .ToList();
        }
        foreach (var doc in documentList)
        {
            string        fileName       = String.Format("Section{0:000}.docx", doc.DocumentNumber);
            List <Source> documentSource = new List <Source> {
                new Source(new WmlDocument("../../Spec.docx"), doc.Start, doc.Count, true)
            };
            DocumentBuilder.BuildDocument(documentSource, fileName);
        }
#endif

#if Reassemble
        // Re-assemble the parts into a single document.
        sources = new DirectoryInfo(".")
                  .GetFiles("Section*.docx")
                  .Select(d => new Source(new WmlDocument(d.FullName), true))
                  .ToList();
        DocumentBuilder.BuildDocument(sources, "ReassembledSpec.docx");
        using (WordprocessingDocument doc =
                   WordprocessingDocument.Open("ReassembledSpec.docx", true))
        {
            ReferenceAdder.AddToc(doc, "/w:document/w:body/w:p[1]",
                                  @"TOC \o '1-3' \h \z \u", null, null);
        }
#endif
    }