static void Main(string[] args) { //// Load a document we can play with. Document doc = LoadDocument.Default(); DocumentCheckSpelling.DocCheckSpelling(doc); // InlineLists.RunTests(); InlineLists.DetectAll(doc); // Comments.AddToEveryPara(doc); Headers.DetectHeaders(doc); Headers.DetectLineSpacingAfterBullets(doc); Language.LanguageChecker(doc); //// Save to a new file. doc.SaveAs2(Filepath.Full().Replace(".docx", "_2.docx")); //// Keep the console open even when the program has finished. ConsoleC.WriteLine(ConsoleColor.Green, "\nThe program has finished."); Console.ReadLine(); }
//// This function demonstrates that we can add a comment to (nearly) every paragraph if we like. public static void AddToEveryPara(Document doc) { ConsoleC.WriteLine(ConsoleColor.White, "Trying to write a comment on all the paragraphs!"); //// Load the default instance of Document class. // Document doc = LoadDocument.Default(); for (int i = 1; i < doc.Paragraphs.Count; i++) { // Add a comment. try { object text = "This is a comment on Paragraph " + i + "."; doc.Comments.Add(doc.Paragraphs[i].Range, ref text); ConsoleC.WriteLine(ConsoleColor.Green, "Added a comment on Paragraph " + i + "!"); } catch (Exception ex) { ConsoleC.WriteLine(ConsoleColor.Red, "Failed to add a comment to Paragraph " + i + " — " + ex.ToString()); } } //Save to a new file. doc.SaveAs2(Filepath.Full().Replace(".docx", "_2.docx")); }
public static void DetectHeaders(Document doc) { try { for (int i = 1; i < doc.Paragraphs.Count; i++) { Paragraph paragraph = doc.Paragraphs[i]; Style style = paragraph.get_Style() as Style; int position = paragraph.ParaID; string styleName = style.NameLocal; string text = paragraph.Range.Text; ConsoleC.Write(ConsoleColor.DarkCyan, styleName + " "); ConsoleC.Write(ConsoleColor.Cyan, position + " "); ConsoleC.WriteLine(ConsoleColor.Blue, StringMethods.TrimAndShorten(text, 20)); ////This checks the spacing after every paragraph. //if (position == 360681186) //{ // ConsoleC.WriteLine(ConsoleColor.Cyan, "Left indent: "+paragraph.Format.LeftIndent); //} //This checks the heading size. if (styleName == "Heading 1") { ConsoleC.WriteLine(ConsoleColor.Green, "Acceptable heading size."); //// Check whether the first-level heading is followed by //// either a second-level heading or a 3–4 lines of body text. DetectParaAfterHeadingOne(doc, i); } else if (styleName == "Heading 2") { ConsoleC.WriteLine(ConsoleColor.Green, "Acceptable heading size."); } else if (styleName == "Heading 3") { ConsoleC.WriteLine(ConsoleColor.Green, "Acceptable heading size."); } else if (styleName == "Heading 4") { ConsoleC.WriteLine(ConsoleColor.Green, "Acceptable heading size."); } else if (styleName == "Heading 5" || styleName == "Heading 6") { ConsoleC.WriteLine(ConsoleColor.Red, "Header is too small."); Comments.Add(doc, paragraph, "Heading level should be 1, 2, 3, or 4, but this is a " + styleName + "."); } } } catch (Exception ex) { ConsoleC.WriteLine(ConsoleColor.Red, "Error in DetectHeaders() — " + ex.ToString()); } }
//// Assuming the 'k'th paragraph in Document 'doc' is a first-level heading. //// Every first-level heading should be followed by either a second-level heading, //// circa 3 or 4 lines of body text and then a second-level heading, //// or body text and no subheading. public static void DetectParaAfterHeadingOne(Document doc, int k) { ConsoleC.WriteLine(ConsoleColor.White, "Checking what’s after the heading..."); try { Paragraph paragraph2 = doc.Paragraphs[k + 1]; try { Style style2 = paragraph2.get_Style() as Style; string styleName2 = style2.NameLocal; if (styleName2 == "Heading 2") { ConsoleC.WriteLine(ConsoleColor.Green, "First-level heading is followed by a second-level heading — good!"); } //// Assuming 75 characters per line. else if (paragraph2.Range.Text.Length > 150 && paragraph2.Range.Text.Length < 375) { ConsoleC.WriteLine(ConsoleColor.Green, "First-level heading is followed by circa 3 or 4 lines of " + styleName2 + " — good!"); } else { try { Paragraph paragraph3 = doc.Paragraphs[k + 2]; Style style3 = paragraph3.get_Style() as Style; string styleName3 = style3.NameLocal; if (styleName3 == "Heading 2") { ConsoleC.WriteLine(ConsoleColor.Blue, paragraph2.Range.Text); string message = "After a first-level heading and before a second-level heading, there should be circa 3 or 4 lines of body text or none."; ConsoleC.WriteLine(ConsoleColor.Red, message); Comments.Add(doc, paragraph2, message); } else { ConsoleC.WriteLine(ConsoleColor.Green, "First-level heading is followed by " + styleName2 + " and " + styleName3 + " — good!"); } } catch { ConsoleC.WriteLine(ConsoleColor.Green, "First-level heading is followed by " + styleName2 + " — good!"); } } } catch { ConsoleC.WriteLine(ConsoleColor.Red, "Something failed with the style detection."); } } catch { ConsoleC.WriteLine(ConsoleColor.Red, "The last paragraph in the document is should not be a heading."); Comments.Add(doc, doc.Paragraphs[k], "The last paragraph in the document should not be a heading."); } }
//// Add() is overloaded such that it will accept either a Document, Paragraph, and String, //// or a Document, int, and string. The string is coerced into an object to work with interop. //// If the second parameter is an int 'k', it is treated as referring to the 'k'th word in the Document. public static void Add(Document doc, Paragraph placeForComment, object comment) { try { ConsoleC.Write(ConsoleColor.White, "\nAdding a comment — "); ConsoleC.WriteLine(ConsoleColor.Blue, comment); doc.Comments.Add(placeForComment.Range, ref comment); } catch { ConsoleC.WriteLine(ConsoleColor.Red, "Failed to add a comment to paragraph!"); } }
//// This checks a document for all paragraphs containing inline lists. public static void DetectAll(Document doc) { ConsoleC.WriteLine(ConsoleColor.White, "Checking every paragraph for inline lists..."); foreach (Paragraph para in doc.Paragraphs) { if (IsMatch(para.Range.Text)) { Comments.Add(doc, para, "This paragraph seems to contain a list. Consider rephrasing as a bulleted or numbered list."); ConsoleC.WriteLine(ConsoleColor.Blue, para.Range.Text); ConsoleC.WriteLine(ConsoleColor.Red, "This paragraph seems to contain a list. Consider rephrasing as a bulleted or numbered list."); } } ConsoleC.WriteLine(ConsoleColor.White, "The check for inline lists is complete."); }
public static void Add(Document doc, int k, object comment) { try { ConsoleC.Write(ConsoleColor.White, "\nAdding a comment of "); ConsoleC.Write(ConsoleColor.Blue, comment); ConsoleC.WriteLine(ConsoleColor.White, " to word #" + k + "."); doc.Comments.Add(doc.Words[k], ref comment); } catch { ConsoleC.WriteLine(ConsoleColor.Red, "Failed to add a comment to word #" + k + "!"); } }
// private static InteropManager im = new InteropManager(Filepath.Folder(), Filepath.FileOnly()); // private static Word.Application wordDoc = im.getWord(); // private static Document doc = wordDoc.Application.ActiveDocument; // Document wordDoc = im.getWord(); //public Headers() //{ //} public static void DetectLineSpacingAfterBullets(Document doc) { ConsoleC.WriteLine(ConsoleColor.White, "Checking every bullet for 6pt line-spacing between indentation levels..."); int badSpacingCount = 0; int badSpacingFailCount = 0; // foreach (Paragraph paragraph in wordDoc.Application.ActiveDocument.Paragraphs) for (int i = 1; i < doc.Paragraphs.Count; i++) { Paragraph paragraph = doc.Paragraphs[i]; Paragraph paragraph2 = doc.Paragraphs[i + 1]; string listString = paragraph.Range.ListFormat.ListString; string listString2 = paragraph2.Range.ListFormat.ListString; if (listString != "" && listString2 != "" && paragraph.Format.LeftIndent != paragraph2.Format.LeftIndent) { Style style = paragraph.get_Style() as Style; string styleName = style.NameLocal; if (styleName != "Heading 1" && styleName != "Heading 2" && styleName != "Heading 3" && styleName != "Heading 4") { if (paragraph.Format.SpaceAfter == 6) { //Console.WriteLine(paragraph.Range.Text); //Console.WriteLine("That's the correct spacing"); } else { ConsoleC.WriteLine(ConsoleColor.Blue, paragraph.Range.Text); //Console.WriteLine("This paragraph's left indent is different to the next paragraph's left indent."); ConsoleC.WriteLine(ConsoleColor.Yellow, "Detected line-spacing that should be 6pt but isn’t."); badSpacingCount++; try { paragraph.Format.SpaceAfter = 6; ConsoleC.WriteLine(ConsoleColor.Green, "Spacing has been changed to 6pt."); Comments.Add(doc, paragraph, "Line-spacing has been changed to 6pt."); } catch { ConsoleC.WriteLine(ConsoleColor.Red, "Failed to automatically change line-spacing to 6pt."); Comments.Add(doc, paragraph, "Line-spacing needs to change to 6pt."); badSpacingFailCount++; } } } else { //ConsoleC.WriteLine(ConsoleColor.Green, "This paragraph is a heading."); } } } //// Give feedback having gone through the document. ConsoleC.WriteLine(ConsoleColor.White, "Finished checking every bullet."); ConsoleC.WriteLine( (badSpacingCount == 0 ? ConsoleColor.Green : ConsoleColor.Yellow), "There were " + badSpacingCount + " instances where the spacing after a bullet " + "needed to be changed to 6pt before a bullet of a different indentation." ); ConsoleC.WriteLine( (badSpacingFailCount == 0 ? ConsoleColor.Green : ConsoleColor.Red), "There are " + badSpacingFailCount + " instances where this could not be corrected automatically." ); //// Save to a new file. doc.SaveAs2(Filepath.Full().Replace(".docx", "_2.docx")); }
public static void LanguageChecker(Document doc) { try { int count = doc.Words.Count; int countUKEnglish = 0; int countUSEnglish = 0; int countNotUKUSEnglish = 0; ConsoleC.WriteLine(ConsoleColor.White, "Checking the language of every word..."); for (int k = 1; k <= count; k++) { //// Write a marker of where we are in the document every kth word. if (k % 50 == 0) { ConsoleC.Write(ConsoleColor.Black, ConsoleColor.Gray, " " + k + " / " + count + " "); } string text = doc.Words[k].Text; //// Check language if (doc.Words[k].LanguageID == WdLanguageID.wdEnglishUK) { ConsoleC.Write(ConsoleColor.Green, text); countUKEnglish++; // ConsoleC.WriteLine(ConsoleColor.Green, "\nThis is a UK/US English word."); } else if (doc.Words[k].LanguageID == WdLanguageID.wdEnglishUS) { ConsoleC.Write(ConsoleColor.Yellow, text); countUSEnglish++; if (countUSEnglish % 10 == 1) { Comments.Add(doc, k, "This is US English but should be UK English."); } } else { ConsoleC.WriteLine(ConsoleColor.Red, "\n" + text); ConsoleC.WriteLine(ConsoleColor.Red, "This is not a UK or US English word."); countNotUKUSEnglish++; if (countNotUKUSEnglish % 10 == 1) { Comments.Add(doc, k, "This is not UK English but should be."); } } } //// Give feedback after all the words have been checked. ConsoleC.WriteLine(ConsoleColor.White, "\nFinished checking language."); ConsoleC.WriteLine(ConsoleColor.Green, countUKEnglish + " words were UK English. This is good!"); if (countUSEnglish > 0) { ConsoleC.WriteLine(ConsoleColor.Yellow, countUSEnglish + " words were US English. Please change these to UK English."); } if (countNotUKUSEnglish > 0) { ConsoleC.WriteLine(ConsoleColor.Red, countNotUKUSEnglish + " words were neither. Please change these to UK English."); } } catch (Exception ex) { ConsoleC.WriteLine(ConsoleColor.Red, ex.ToString()); } }