I have never programmed in C# before or used Aspose.words before. I was asked to write a program for Aspose.words to update a large amount of refences in doc files, and convert docx to doc files. I was also asked to write one to read the references from the word documents into an excel spreadsheet. Create a list of references with the associated file name from the document they were originally in. This way the references could be updated in excel. Then create a program to update all off the references in the documents based off the updates in the excel file. I have written what I believe will work for the first problem. I was hoping somone could review it and tell me if I am making in obvious mistakes and if there was a way to improve its speed as it will be looking at many documents. Also I was wondering if there was any advice or programs already written that might solve the second two problems dealing with excel spreadsheets. Thank you for any and all help.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Aspose.Words;
namespace TextReplaceDemo
{
class Program
{
static void Main(string[] args)
{
// Set license info
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense("Aspose.Total.lic");
string path = Directory.GetCurrentDirectory();
// Get all docx files in the search location, we will need to convert them to doc files first
DirectoryInfo directoryInfo = new DirectoryInfo(path + @"\In\");
FileInfo[] docxfiles = directoryInfo.GetFiles("*.docx", SearchOption.AllDirectories);
// Create a directory for backups
if (Directory.Exists(path + @"\Backup\"))
Directory.Delete(path + @"\Backup\", true);
Directory.CreateDirectory(path + @"\Backup\");
// Create a directory to save files converted from Docx to Doc
if (Directory.Exists(path + @"\In\DocxtoDoc"))
Directory.Delete(path + @"\In\DocxtoDoc", true);
Directory.CreateDirectory(path + @"\In\DocxtoDoc");
// Create a directory for completed files
if (Directory.Exists(path + @"\Out"))
Directory.Delete(path + @"\Out", true);
Directory.CreateDirectory(path + @"\Out");
foreach (FileInfo file in docxfiles) //Go through all the docx files…
{
try
{
file.CopyTo(path + @"\Backup\" + file.Name); //Create a backup of docx file
Document doc = new Document(file.FullName); //Open docx file
doc.Save(path + @"\In\DocxtoDoc\" + file.Name, SaveFormat.Doc); //Save file as doc
}
catch (Exception ex) // On Error displace exception
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
// Get all doc files in the search location
FileInfo[] docfiles = directoryInfo.GetFiles("*.doc", SearchOption.AllDirectories);
// Loop over the files
foreach (FileInfo file in docfiles) //Go through all the doc files....
{
try
{
bool isModified = false;
// Make a back up of the file in case we mess something up
file.CopyTo(path + @"\Backup\" + file.Name);
Document doc = new Document(file.FullName);
// Grab all the paragraphs in the document and loop over them
NodeCollection nc = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Node n in nc)
{
Paragraph thisPara = (Paragraph)n;
// Check each paragraph to see if is a reference by examining the assigned style.
// If it is and it is any variation of a reference to a Standard Spec, update it
// to a 2014 reference.
if (thisPara.ParagraphFormat.StyleName.Contains("Reference Style"))
{
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0000"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 0000 (SFLC Std Spec 0000), 2014, General Requirements", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0850"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 0085 (SFLC Std Spec 0085), 2014, General Requirements for Drawing Preparation", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0085"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 0085 (SFLC Std Spec 0085), 2014, General Requirements for Drawing Preparation", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0450"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 0450 (SFLC Std Spec 0450), 2014, Electrical Power for Contractor's Tools & Equipment", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0740"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 0740 (SFLC Std Spec 0740), 2014, Welding and Allied Processes", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("2350"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 2350 (SFLC Std Spec 2350), 2014, Inspect, Test and Recondition DC Propulsion Generators & Motors In-Place", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("2351"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 2351 (SFLC Std Spec 2351), 2014, Overhaul DC Generators & Motors", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3020"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 3020 (SFLC Std Spec 3020), 2014, Overhaul AC Electrical Motors", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3041"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 3041 (SFLC Std Spec 3041), 2014, Shipboard Electrical Cable Test", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3042"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 3042 (SFLC Std Spec 3042), 2014, Shipboard Electrical Cable Removal, Relocation, Splice, Repair, and Installation", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3100"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 3100 (SFLC Std Spec 3100), 2014, Inspect, Test and Recondition AC Sychronous Machines In-Place", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3101"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 3101 (SFLC Std Spec 3101), 2014, Overhaul AC Synchronous Machines", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("5000"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 5000 (SFLC Std Spec 5000), 2014, Auxiliary Machine Systems", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("5100"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 5100 (SFLC Std Spec 5100), 2014, Clean Shipboard Ventilation Systems", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("6310"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 6310 (SFLC Std Spec 6310), 2014, Requirements for Preservation of Ship Structures", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("6341"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 6341 (SFLC Std Spec 6341), 2014, Install Interior Deck Covering Systems", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("8634"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 8634 (SFLC Std Spec 8634), 2014, Drydocking", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("8635"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 8635 (SFLC Std Spec 8635), 2014, Temporary Services", false, false);
isModified = true;
}
if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("8636"))
{
n.Range.Replace(n.Range.Text.Replace("\r", ""), "Surface Forces Logistics Center Standard Specification 8636 (SFLC Std Spec 8636), 2014, Temporary Hull Accesses", false, false);
isModified = true;
}
}
// Another random replacement sample.
// if (n.ToTxt().Contains("in accordance with COMDTINST M10360.3, Appendix A (Cutter and Boat Exterior Painting Systems"))
// {
// n.Range.Replace("Appendix A", "Appendix D", false, false);
// isModified = true;
// }
}
// Save new version if we made any replacements.
if (isModified)
{
doc.Save(path + @"\Out\" + file.Name);
}
}
catch (Exception ex) // On Error displace exception
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
}