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.