Reading Coreldraw Files In C# Winforms

In this chapter, I will explain to you how to read CorelDRAW(.cdr) Files and convert to a different format. To read .cdr files, CorelDraw software should be installed in your system.
 
Add interop.CorelDraw.dll through references as shown in the below image,

 
Include the reference in your class as shown in the below image,

 
 
Use open file dialog, and select .cdr files.

 

Click on select file button and include the below code to select .cdr files.
  1. private void btnSelect_Click(object sender, EventArgs e)  
  2.        {  
  3.            try  
  4.            {  
  5.                frmLoadImage frmImage = new frmLoadImage();  
  6.                frmLoadPicture frmPic = new frmLoadPicture();  
  7.                ofdCorel.InitialDirectory = "C.";   
  8.                ofdCorel.Title = "Open a Corel Draw file";  
  9.                ofdCorel.Filter = "Corel Draw files|*.cdr";  
  10.                ofdCorel.Multiselect = true;  
  11.                if (ofdCorel.ShowDialog() == DialogResult.Cancel)  
  12.                {  
  13.                    MessageBox.Show("File selection cancelled");  
  14.                }  
  15.                else  
  16.                {  
  17.                    convertCDRFiles();                    
  18.                }  
  19.            }  
  20.            catch (Exception ex)  
  21.            {  
  22.                 MessageBox.Show(ex.HResult + " - " + ex.Message ,"CorelDraw",MessageBoxButtons.OK,MessageBoxIcon.Error);                 
  23.            }  
  24.       }  
If file selection is successful then it will call convertCDRFiles method,
  1. private void convertCDRFiles()    
  2.         {    
  3.             foreach (String file in ofdCorel.FileNames)    
  4.             {    
  5.                 selectedFile = file;    
  6.                 strFilePath = System.IO.Path.GetDirectoryName(selectedFile);    
  7.                 strFileName = System.IO.Path.GetFileNameWithoutExtension(selectedFile);    
  8.     
  9.                 txtFilePath.Text = selectedFile;    
  10.                 strFilePath = strFilePath + "\\Output";    
  11.                 CheckAndCreateFolder(strFilePath);    
  12.                 strExpFileName = strFilePath + "\\" + strFileName;    
  13.                 CorelDRAW.Application cdr = new CorelDRAW.Application();    
  14.                 cdr.Visible = false;    
  15.                 cdr.OpenDocument(selectedFile, 1);    
  16.                 cdr.ActiveLayer.Activate();    
  17.                 strActiveLayer = cdr.ActiveLayer.Name.ToString();    
  18.                 cdr.ActiveDocument.Unit = cdrUnit.cdrPoint;    
  19.     
  20.                 foreach (Shape s in cdr.ActiveLayer.Shapes)    
  21.                 {    
  22.                     if (s.Type == CorelDRAW.cdrShapeType.cdrTextShape)    
  23.                     {    
  24.                         strTextNames = strTextNames + "Shape Name :" + s.Type.ToString() + " |Text Value :" + s.Text.Story.Text + System.Environment.NewLine    
  25.                             + "LeftX: " + s.LeftX + "| RightX: " + s.RightX + System.Environment.NewLine    
  26.                             + "Top Y: " + s.TopY + "| Bottom Y: " + s.BottomY + System.Environment.NewLine    
  27.                             + "Center X: " + s.CenterX + "| Center Y: " + s.CenterY + System.Environment.NewLine    
  28.                             + "Height :" + s.OriginalHeight + "| Width : " + s.OriginalWidth + System.Environment.NewLine;    
  29.                     }    
  30.                 }    
  31.                    
  32.                 strFiltType = strExpFileName + ".tif";    
  33.                 if (blnIsSaveFile(strFiltType))    
  34.                 {    
  35.                     blnExpFlag = true;    
  36.                     cdr.ActiveDocument.Export(strFiltType, cdrFilter.cdrTIFF, cdrExportRange.cdrCurrentPage, expOption);    
  37.                 }    
  38.                 strFiltType = strExpFileName + ".png";    
  39.                 if (blnIsSaveFile(strFiltType))    
  40.                 {    
  41.                     blnExpFlag = true;    
  42.                     cdr.ActiveDocument.Export(strFiltType, cdrFilter.cdrPNG, cdrExportRange.cdrCurrentPage, expOption);    
  43.                 }    
  44.                 strFiltType = strExpFileName + ".jpeg";    
  45.                 if (blnIsSaveFile(strFiltType))    
  46.                 {    
  47.                     blnExpFlag = true;    
  48.                     cdr.ActiveDocument.Export(strFiltType, cdrFilter.cdrJPEG, cdrExportRange.cdrCurrentPage, expOption);    
  49.                 }    
  50.                 //---------------------Export to PDF ----------------------------------//    
  51.                 cdr.ActiveDocument.PDFSettings.Author = "Corel Corporation";    
  52.                 cdr.ActiveDocument.PDFSettings.Bookmarks = true;    
  53.                 cdr.ActiveDocument.PDFSettings.ColorMode = VGCore.pdfColorMode.pdfRGB;    
  54.                 cdr.ActiveDocument.PDFSettings.ComplexFillsAsBitmaps = false;    
  55.                 cdr.ActiveDocument.PDFSettings.CompressText = true;    
  56.                 cdr.ActiveDocument.PDFSettings.CropMarks = false;    
  57.                 cdr.ActiveDocument.PDFSettings.DownsampleGray = true;    
  58.                 cdr.ActiveDocument.PDFSettings.EmbedBaseFonts = true;    
  59.                 cdr.ActiveDocument.PDFSettings.EmbedFonts = true;    
  60.                 cdr.ActiveDocument.PDFSettings.FileInformation = true;    
  61.                 cdr.ActiveDocument.PDFSettings.Hyperlinks = true;    
  62.                 cdr.ActiveDocument.PDFSettings.Keywords = "Test, Example, Corel, CorelDRAW, PublishToPDF";    
  63.                 cdr.ActiveDocument.PDFSettings.Linearize = true;    
  64.                 cdr.ActiveDocument.PDFSettings.PageRange = "1";    
  65.                 cdr.ActiveDocument.PDFSettings.pdfVersion = VGCore.pdfVersion.pdfVersion13;    
  66.                 cdr.ActiveDocument.PDFSettings.PublishRange = VGCore.pdfExportRange.pdfPageRange;    
  67.                 cdr.ActiveDocument.PDFSettings.TrueTypeToType1 = true;    
  68.                 strFiltType = strExpFileName + ".pdf";    
  69.                 if (blnIsSaveFile(strFiltType))    
  70.                 {    
  71.                     blnExpFlag = true;    
  72.                     cdr.ActiveDocument.PublishToPDF(strFiltType);    
  73.                 }    
  74.                 //-------------------------Writing tText box coordinates to txt file-------------------------    
  75.                 strFiltType = strExpFileName + ".txt";    
  76.                 if (blnIsSaveFile(strFiltType))    
  77.                 {    
  78.                     blnExpFlag = true;    
  79.                     System.IO.StreamWriter txtfile = new System.IO.StreamWriter(strFiltType);    
  80.                     txtfile.WriteLine(strTextNames);    
  81.                     txtfile.Close();    
  82.                 }    
  83.                 cdr.Quit();    
  84.                 txtFilePath.Text = "";    
  85.             }    
  86.             if (blnExpFlag)    
  87.             {    
  88.                 this.Hide();    
  89.                 MessageBox.Show("Files exported sucessfully""CorelDraw", MessageBoxButtons.OK, MessageBoxIcon.Information);    
  90.             }    
  91.         }    
  92. private bool blnIsSaveFile(string strFileName)    
  93.        {    
  94.            bool blnRet = true;    
  95.            try    
  96.            {    
  97.                if (File.Exists(strFiltType) == true)    
  98.                {    
  99.                    string strFile = System.IO.Path.GetFileName(strFileName);    
  100.                    if (MessageBox.Show(strFile + " already exists." + System.Environment.NewLine + "Do you want to replace it?""Confirm Save As", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)    
  101.                    {    
  102.                        File.Delete(strFiltType);    
  103.                    }    
  104.                    else    
  105.                    {    
  106.                        blnRet = false;    
  107.                    }    
  108.                }                    
  109.            }    
  110.            catch (Exception ex)    
  111.            {    
  112.                blnRet = false;    
  113.                MessageBox.Show(ex.HResult + " - " + ex.Message, "CorelDraw", MessageBoxButtons.OK, MessageBoxIcon.Error);    
  114.            }    
  115.            return blnRet;    
  116.        }  
You can change the output file folder or you can give an option as user input.. In the above example, I will check folder as "OUTPUT " in input file selection folder and if it is not available it will create the folder as Output and converted files will be saved in output folder.
 
The output file name is the same as input file name with its file type extension. All output files look the same as input file except .txt file. 
 
The .txt file consists of text box information which is available in an active layer of CorelDRAW file. We can customize the format the way we need.
 
Note
If CorelDRAW is not installed in your machine it throws an error. So try after installing CorelDRAW software...
 
If anyone knows how to achieve the same without installing CorelDRAW software package, please reply on this post.
Next Recommended Reading Numeric Keypad Functionality in WinForms