Corel to Illustrator Conversion in Visual Studio C#

I am no artist and so Clipart is my friend. But I have had difficulty finding as great of clipart libraries, as that of the original Corel Draw Gallery and the Corel Gallery 1 Million. I have not used Corel Draw since version 9, choosing instead to use Adobe Illustrator. But to use the old Corel Clipart was a clunky process and conversion to AI had aluded me – until now. I wanted to share this collection with my daughter, so I relooked at using c# to convert CDR to AI, and here is what I found.

1. There is an SK1 uniconverter library on the web that can be run on the command line. This does not require Corel Draw on your system and allows for multiple outputs other than ai. Unfortunately, it cannot convert all cdr files and some conversions have errors. Still it worked fairly well and the c# implementation is here:


private bool convertVectorFile(string sourceFile, string destFile)
{
try
{
using (Process proc = new Process()) {
string targetDir = string.Format(@"C:\Program Files (x86)\sK1 Project\UniConvertor-1.1.5");
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = @"C:\Program Files (x86)\sK1 Project\UniConvertor-1.1.5\uniconvertor.cmd";
proc.StartInfo.Arguments = string.Format("{0} {1}",sourceFile,destFile);
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit(10000);
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()));
}
return false;

}

2. A more accurate conversion is by using Corel Draw to make the conversion. I still have a copy of Corel Draw 9. I just referenced the Corel Draw 9 type Library in the References-Com tab. Note that destFile has the file extension ‘ai’, and corel draw will choose that format. I then call the following code:


private bool convertCdrFile(string sourceFile, string destFile)
{
bool bOk = false;
CorelDRAW.Application app = null;

try
{
app = new CorelDRAW.Application();
app.Visible = true;
CorelDRAW.Document doc = app.Open(sourceFile);

doc.SaveAs(destFile);

doc.Dirty = false;
doc.Close();
doc = null;
bOk = true;
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()));
}
if (app!=null) app.Quit();
app = null;
return bOk;

}

3. The code to walk the directories and convert each file is straight forward. I would just pre-create the directory structure first for the destination. Note that I order these in order of shortest to longest path so that I ensure that I create parent directories at the base of the tree first, and then their children:

var subDirs = sourceDir.GetDirectories("*.*", System.IO.SearchOption.AllDirectories.OrderBy(s => s.FullName.Length);
foreach (DirectoryInfo sourceSubDir in subDirs)
{
string destSubName = tbDestDir.Text + sourceSubDir.FullName.Substring(sourceLength);
if (!Directory.Exists(destSubName))
Directory.CreateDirectory(destSubName);
}

4. I then walk each directory and call the Cdr convert method above:


string[] _vectorExt = { "CDR", "CCX", "CDT", "CDRX", "CMX", "WMF", "SVG", "CGM", "EPS", "PS" };

foreach (FileInfo fileSource in sourceSubDir.GetFiles())
{
string filename = System.IO.Path.GetFileNameWithoutExtension(fileSource.Name);
string ext = fileSource.Extension.TrimStart('.').ToUpper();
string fileDest = System.IO.Path.Combine(destSubName, filename);

if (!File.Exists(fileDest))
{
if (_vectorExt.Contains(ext))
{
fileDest += ".ai";
convertCdrFile(fileSource.FullName, fileDest);
}

}

}
5. The above code does actually generate readable a1 files, but they cannot be visualized in Bridge. However, if we open each file in illustrator and perform a save, then this converts each file to a current ai format and they appear well in bridge. Note that I had to add the reference to adobe illustrator type library in com. I re-fit the art board to each image to maximize their appearance in bridge.

private bool convertAIFile(string sourceFile, string destFile)
{
Illustrator.Application app = new Illustrator.Application();
try
{
Illustrator.Document doc = app.Open(sourceFile, Illustrator.AiDocumentColorSpace.aiDocumentRGBColor, null);
doc.SelectObjectsOnActiveArtboard();
var iArtBoard = doc.Artboards.GetActiveArtboardIndex();
doc.FitArtboardToSelectedArt(iArtBoard);
doc.SaveAs(destFile);
doc.Close();
doc = null;

return true;
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()));
}
app.Quit();
app = null;

return false;

}

Advertisements
This entry was posted in Coding, Uncategorized. Bookmark the permalink.