0

We have hundreds of Illustrator files that use a font that needs to be changed. I can open the Illustrator file in Notepad++, search for the font in question and replace it with the new font. The file then opens without issue in Illustrator using the correct font.

I'm trying to write a bit of C# code to do this automatically, but Illustrator claims that the resulting file is corrupted.

I assumed this was due to the encoding of the file, so I've tried reading and writing the file with various different encodings and the result is basically the same. The file either won't open at all, or it is blank white page. Either way, Illustrator says the file is corrupted.

After several revisions, here is my code as it stands right now:

public void RenameFont()
{
    DirectoryInfo directory = new DirectoryInfo(Common.SOURCE_FOLDER);
    FileInfo[] files = directory.GetFiles("*.ai", SearchOption.TopDirectoryOnly);
    foreach (FileInfo file in files)
    {
        string fileName = file.Name.Replace(".ai", "");
        if (int.TryParse(fileName, out int itemNumber))
        {
            using (FileStream fs = file.OpenRead())
            using (StreamReader reader = new StreamReader(fs, Encoding.GetEncoding(1252)))
            {
                string line;
                        
                using (StreamWriter writer = new StreamWriter(File.Open(Path.Combine(file.DirectoryName, $"new{file.Name}"), FileMode.Create), Encoding.GetEncoding(1252)))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        foreach (string badFont in Common.SEARCH_FOR)
                        {   
                            if (line.Contains(badFont))
                            {
                                line = line.Replace(badFont, Common.REPLACE_WITH);
                            }
                        }
                        writer.WriteLine(line);
                    }
                    writer.Flush();
                    writer.Close();
                }
                reader.Close();
            }
        }
    }
}

Even if I just read and write the file without modifying the font name, the result is still the same.

EDIT: I wound up using Notepad++'s "Find in Files" feature to replace the text. It handled all the files in less than a minute and they all open in Illustrator without issue.

  • A grep tool could do this task much more reliably and easily. Don't re-invent the wheel – ADyson Oct 07 '20 at 17:35
  • Have you tried plain ASCII encoding? Or, at last resource, open the file as binary and work with bytes, old school low level. – aamartin2k Oct 07 '20 at 17:47
  • @ADyson I feel foolish. While I didn't use a grep tool, I was able to use Notepad++'s "Find in File" feature to replace the text for me. Worked like a champ and took less than a minute. Man, I feel like an idiot for not thinking of that. – Jimmy Scrambles Oct 07 '20 at 18:17

1 Answers1

1

A grep tool could do this task much more reliably and easily. Don't re-invent the wheel – ADyson

While a grep tool was a great idea (I still don't know why I didn't think of it). I realized that Notepad++ had a Find in Files feature that could do the text replace for me.

I had it run through all the files in the folder and do a find/replace where needed. Took less than a minute and everything is working great.