protected override void ProcessRecord() { foreach (var document in AllDocuments("Add-OpenXmlDocumentTOF")) { try { if (!(document is WmlDocument)) { throw new PowerToolsDocumentException("Not a wordprocessing document."); } OutputDocument(ReferenceAdder.AddTof((WmlDocument)document, InsertionPoint, Switches, null)); } catch (Exception e) { WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document)); } } }
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); } }