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.
- private void btnSelect_Click(object sender, EventArgs e)
- {
- try
- {
- frmLoadImage frmImage = new frmLoadImage();
- frmLoadPicture frmPic = new frmLoadPicture();
- ofdCorel.InitialDirectory = "C.";
- ofdCorel.Title = "Open a Corel Draw file";
- ofdCorel.Filter = "Corel Draw files|*.cdr";
- ofdCorel.Multiselect = true;
- if (ofdCorel.ShowDialog() == DialogResult.Cancel)
- {
- MessageBox.Show("File selection cancelled");
- }
- else
- {
- convertCDRFiles();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.HResult + " - " + ex.Message ,"CorelDraw",MessageBoxButtons.OK,MessageBoxIcon.Error);
- }
- }
If file selection is successful then it will call convertCDRFiles method,
- private void convertCDRFiles()
- {
- foreach (String file in ofdCorel.FileNames)
- {
- selectedFile = file;
- strFilePath = System.IO.Path.GetDirectoryName(selectedFile);
- strFileName = System.IO.Path.GetFileNameWithoutExtension(selectedFile);
-
- txtFilePath.Text = selectedFile;
- strFilePath = strFilePath + "\\Output";
- CheckAndCreateFolder(strFilePath);
- strExpFileName = strFilePath + "\\" + strFileName;
- CorelDRAW.Application cdr = new CorelDRAW.Application();
- cdr.Visible = false;
- cdr.OpenDocument(selectedFile, 1);
- cdr.ActiveLayer.Activate();
- strActiveLayer = cdr.ActiveLayer.Name.ToString();
- cdr.ActiveDocument.Unit = cdrUnit.cdrPoint;
-
- foreach (Shape s in cdr.ActiveLayer.Shapes)
- {
- if (s.Type == CorelDRAW.cdrShapeType.cdrTextShape)
- {
- strTextNames = strTextNames + "Shape Name :" + s.Type.ToString() + " |Text Value :" + s.Text.Story.Text + System.Environment.NewLine
- + "LeftX: " + s.LeftX + "| RightX: " + s.RightX + System.Environment.NewLine
- + "Top Y: " + s.TopY + "| Bottom Y: " + s.BottomY + System.Environment.NewLine
- + "Center X: " + s.CenterX + "| Center Y: " + s.CenterY + System.Environment.NewLine
- + "Height :" + s.OriginalHeight + "| Width : " + s.OriginalWidth + System.Environment.NewLine;
- }
- }
-
- strFiltType = strExpFileName + ".tif";
- if (blnIsSaveFile(strFiltType))
- {
- blnExpFlag = true;
- cdr.ActiveDocument.Export(strFiltType, cdrFilter.cdrTIFF, cdrExportRange.cdrCurrentPage, expOption);
- }
- strFiltType = strExpFileName + ".png";
- if (blnIsSaveFile(strFiltType))
- {
- blnExpFlag = true;
- cdr.ActiveDocument.Export(strFiltType, cdrFilter.cdrPNG, cdrExportRange.cdrCurrentPage, expOption);
- }
- strFiltType = strExpFileName + ".jpeg";
- if (blnIsSaveFile(strFiltType))
- {
- blnExpFlag = true;
- cdr.ActiveDocument.Export(strFiltType, cdrFilter.cdrJPEG, cdrExportRange.cdrCurrentPage, expOption);
- }
-
- cdr.ActiveDocument.PDFSettings.Author = "Corel Corporation";
- cdr.ActiveDocument.PDFSettings.Bookmarks = true;
- cdr.ActiveDocument.PDFSettings.ColorMode = VGCore.pdfColorMode.pdfRGB;
- cdr.ActiveDocument.PDFSettings.ComplexFillsAsBitmaps = false;
- cdr.ActiveDocument.PDFSettings.CompressText = true;
- cdr.ActiveDocument.PDFSettings.CropMarks = false;
- cdr.ActiveDocument.PDFSettings.DownsampleGray = true;
- cdr.ActiveDocument.PDFSettings.EmbedBaseFonts = true;
- cdr.ActiveDocument.PDFSettings.EmbedFonts = true;
- cdr.ActiveDocument.PDFSettings.FileInformation = true;
- cdr.ActiveDocument.PDFSettings.Hyperlinks = true;
- cdr.ActiveDocument.PDFSettings.Keywords = "Test, Example, Corel, CorelDRAW, PublishToPDF";
- cdr.ActiveDocument.PDFSettings.Linearize = true;
- cdr.ActiveDocument.PDFSettings.PageRange = "1";
- cdr.ActiveDocument.PDFSettings.pdfVersion = VGCore.pdfVersion.pdfVersion13;
- cdr.ActiveDocument.PDFSettings.PublishRange = VGCore.pdfExportRange.pdfPageRange;
- cdr.ActiveDocument.PDFSettings.TrueTypeToType1 = true;
- strFiltType = strExpFileName + ".pdf";
- if (blnIsSaveFile(strFiltType))
- {
- blnExpFlag = true;
- cdr.ActiveDocument.PublishToPDF(strFiltType);
- }
-
- strFiltType = strExpFileName + ".txt";
- if (blnIsSaveFile(strFiltType))
- {
- blnExpFlag = true;
- System.IO.StreamWriter txtfile = new System.IO.StreamWriter(strFiltType);
- txtfile.WriteLine(strTextNames);
- txtfile.Close();
- }
- cdr.Quit();
- txtFilePath.Text = "";
- }
- if (blnExpFlag)
- {
- this.Hide();
- MessageBox.Show("Files exported sucessfully", "CorelDraw", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- private bool blnIsSaveFile(string strFileName)
- {
- bool blnRet = true;
- try
- {
- if (File.Exists(strFiltType) == true)
- {
- string strFile = System.IO.Path.GetFileName(strFileName);
- if (MessageBox.Show(strFile + " already exists." + System.Environment.NewLine + "Do you want to replace it?", "Confirm Save As", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
- {
- File.Delete(strFiltType);
- }
- else
- {
- blnRet = false;
- }
- }
- }
- catch (Exception ex)
- {
- blnRet = false;
- MessageBox.Show(ex.HResult + " - " + ex.Message, "CorelDraw", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- return blnRet;
- }
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.