private VGCore.Shape loadResizedImage(VGCore.Layer layer, string imageFilePath) { double adjustedMaxWidth = MaxWidth; if (imageFilePath.EndsWith(".svg", StringComparison.OrdinalIgnoreCase)) { //if svg file contains only text string svgText = File.ReadAllText(imageFilePath); if (svgText.Contains("<text") && !svgText.Contains("<image")) { return(null); } adjustedMaxWidth = getMaxWidth(imageFilePath); transformImageFile(imageFilePath); } layer.ImportEx(imageFilePath).Finish(); VGCore.Shape image = layer.FindShape(Path.GetFileName(imageFilePath)); if (imageFilePath.EndsWith(".svg", StringComparison.OrdinalIgnoreCase)) { if (image.SizeWidth > adjustedMaxWidth || image.SizeHeight > MaxHeight) { if (image.SizeWidth >= image.SizeHeight) { double diff = (image.SizeWidth - adjustedMaxWidth) / image.SizeWidth; image.SizeWidth = adjustedMaxWidth; image.SizeHeight = (1 - diff) * image.SizeHeight; } else { double diff = (image.SizeHeight - MaxHeight) / image.SizeHeight; image.SizeHeight = MaxHeight; image.SizeWidth = (1 - diff) * image.SizeWidth; } } if (image.SizeWidth > adjustedMaxWidth) { double diff = (image.SizeWidth - adjustedMaxWidth) / image.SizeWidth; image.SizeWidth = adjustedMaxWidth; image.SizeHeight = (1 - diff) * image.SizeHeight; } if (image.SizeHeight > MaxHeight) { double diff = (image.SizeHeight - MaxHeight) / image.SizeHeight; image.SizeHeight = MaxHeight; image.SizeWidth = (1 - diff) * image.SizeWidth; } } // if it's a jpg file of MUG else { image.SizeWidth = MaxHeight; image.SizeHeight = MaxHeight; } return(image); }
//Export function for the White Background public void ExportFmrWBackground() { ShapeRange kit = this.appDraw.ActiveSelectionRange; if (CheckKitDimensions(kit)) { VGCore.Shape fmrw = ImportBackground("FMR_W.cdr"); ArrangeExportAndCleanup(kit, fmrw, cdrFilter.cdrPNG, 2000, 1197); } }
// Exports MSMX background with customizable text labels public void ExportMSMXBackground(string variant, string number = null, string name = null) { VGCore.ShapeRange kit = this.appDraw.ActiveSelectionRange; if (CheckKitDimensions(kit)) { VGCore.Shape msmx = ImportBackground("msmx.cdr"); ArrangeExportAndCleanup(kit, msmx, cdrFilter.cdrJPEG, 1000, 779, variant, number, name); } }
//Exports all FMR Backgrounds public void ExportAllFmrBackgrounds() { ShapeRange kit = this.appDraw.ActiveSelectionRange; if (CheckKitDimensions(kit)) { String[] filePaths = Directory.GetFiles(@"C:\CorelDrawPreviewExport\CorelDrawPreviewExport\assets\bg\fmr\", "*.cdr", SearchOption.TopDirectoryOnly); for (int i = 0; i < filePaths.Length; i++) { VGCore.Shape fmr = ImportBackground(filePaths[i].ToString().Substring(filePaths[i].Length - 9, 9)); ArrangeExportAndCleanup(kit, fmr, cdrFilter.cdrPNG, 2000, 1197); } } }
//exports image private void ExportImage(VGCore.ShapeRange kit, VGCore.Shape background, cdrFilter imageType, int width, int height, string variant = null, string number = null, string name = null) { ShapeRange exportRange = kit; exportRange.Add(background); exportRange.CreateSelection(); String fileName = this.appDraw.ActiveDocument.FileName; String filePath = this.appDraw.ActiveDocument.FilePath; fileName = fileName.Remove(fileName.Length - 4, 4); String suffix = ""; ExportFilter expFil; if (variant == null) { if (background.Name != "FMR_W.cdr") { suffix = " -" + background.Name.ElementAt(4); } if (imageType == cdrFilter.cdrPNG) { suffix += ".png"; } expFil = this.appDraw.ActiveDocument.ExportBitmap(filePath + fileName + suffix, imageType, cdrExportRange.cdrSelection, cdrImageType.cdrRGBColorImage, width, height, 300, 300); } else { if (imageType == cdrFilter.cdrJPEG) { suffix += ".jpg"; } //fileName = variant + fileName.Substring(6); expFil = this.appDraw.ActiveDocument.ExportBitmap(filePath + fileName + suffix, imageType, cdrExportRange.cdrSelection, cdrImageType.cdrRGBColorImage, width, height, 300, 300, cdrAntiAliasingType.cdrNormalAntiAliasing, false, false, true, false, cdrCompressionType.cdrCompressionJPEG); } expFil.Finish(); }
// ArrangeBackground - Arranges Backgrounds behind the decals kit. FMR_K needs to be moved to the bottom left and FMR_M needs to be moved down by a bit. private void ArrangeBackground(ShapeRange kit, VGCore.Shape background, string variant = null, string number = null, string name = null) { background.OrderToBack(); if (background.Name != "FMR_K.cdr") { background.SetSize(kit.SizeWidth + 5); background.CenterX = kit.CenterX; background.CenterY = kit.CenterY; if (background.Name == "FMR_M.cdr") { background.CenterY = kit.CenterY + 1; } } else { background.SetSize(kit.SizeWidth + 15); background.CenterX = kit.CenterX - 5; background.CenterY = kit.CenterY + 2; } if (background.Name == "msmx.cdr") { if (name.Length > 0 || number.Length > 0) { background.Shapes.FindShape("custom", cdrShapeType.cdrTextShape).Text.Replace("#XX XXXXXXXXX", "#" + number + " " + name, true); } else { background.Shapes.FindShape("custom", cdrShapeType.cdrTextShape).Text.Replace("#XX XXXXXXXXX", "", true); } background.Shapes.FindShape("design", cdrShapeType.cdrTextShape).Text.Replace("X XXXX -XX", variant, true); background.CenterY = kit.CenterY + 3; } }
private void generate(IProgress <object> progress) { VGCore.Application application; try { application = new VGCore.Application(); } catch (Exception ex) { throw new Exception("CorelDRAW is not started or its version is not compatible with the software, please start CorelDRAW if not started or install compatible version"); } VGCore.Document document = null; double positionX = 0; double positionY = 0; List <string> processedZipFiles = new List <string>(); string[] zipFiles = getZipFiles(ZipsFolderPathTxt.Text); int zipCount = 1; foreach (string zipFilePath in zipFiles) { if ((zipCount - 1) % 3 == 0) { if (document != null) { saveCloseDocument(document); moveZipFiles(processedZipFiles); } document = newDocument(application); positionX = MarginLeft; //positionY = PageHeight + MaxHeight; positionY = PageHeight - MarginTop + MaxHeight; } VGCore.Layer layer = document.ActiveLayer; List <string> imageFiles = getImagesFromZip(zipFilePath); bool isFirstImage = true; foreach (string imageFilePath in imageFiles) { VGCore.Shape image; try { image = loadResizedImage(layer, imageFilePath); } catch (Exception ex) { string message = "Failed to load file '" + Path.GetFileName(imageFilePath) + "' from zip file '" + Path.GetFileName(zipFilePath); progress.Report(message + "' for template file '" + document.Name + ".cdr'" + Environment.NewLine); //image = layer.CreateArtisticText(0, 0, message); //image.SizeWidth = MaxWidth; //image.SizeHeight = MaxHeight; //image.WrapText = VGCore.cdrWrapStyle.cdrWrapSquareAboveBelow; continue; } double x = 0; double y = 0; if (isFirstImage) { positionX = MarginLeft; //positionY -= MaxHeight + MarginTop; positionY -= MaxHeight; if (image != null) { x = positionX + ((MaxWidth - image.SizeWidth) / 2); y = positionY - (MaxHeight - image.SizeHeight) / 2; } isFirstImage = false; // insert order id from zip file name into the row layer.CreateArtisticText(-150, positionY - 15, Path.GetFileNameWithoutExtension(zipFilePath).Split('_')[0] , VGCore.cdrTextLanguage.cdrLanguageNone, VGCore.cdrTextCharSet.cdrCharSetMixed , "Arial", 30); } else { //positionX = PageWidth - MarginRight - image.SizeWidth; positionX += MaxWidth + MarginRight; if (image != null) { x = positionX + ((MaxWidth - image.SizeWidth) / 2); y = positionY - (MaxHeight - image.SizeHeight) / 2; } } if (image != null) { image.SetPosition(x, y); } // Insert text from xml file Dictionary <string, string> textData; bool hasValue = inputTexts.TryGetValue(Path.GetFileNameWithoutExtension(imageFilePath), out textData); if (hasValue && textData != null && textData["text"] != null && textData["text"].Length > 0) { VGCore.Shape text = layer.CreateArtisticText(positionX, positionY - 25, textData["text"] , VGCore.cdrTextLanguage.cdrLanguageNone, VGCore.cdrTextCharSet.cdrCharSetMixed , textData["family"], 25); String color = textData["color"]; if (color != null && color != "") { //.FromArgb(Convert.ToInt32(color.Replace("#", ""), 16)) VGCore.Color c = layer.Color; c.HexValue = color; text.Fill.ApplyUniformFill(c); } } } progress.Report(((100 - 10) / zipFiles.Length) * zipCount); zipCount++; processedZipFiles.Add(zipFilePath); } saveCloseDocument(document); moveZipFiles(processedZipFiles); application.Refresh(); //emptyTempDirectory(); progress.Report(100); }
//runs ArrangeBackground, ExportImage and deletes background to keep the file sizes low. private void ArrangeExportAndCleanup(VGCore.ShapeRange kit, VGCore.Shape background, cdrFilter filter, int Width, int Height, string variant = null, string number = null, string name = null) { ArrangeBackground(kit, background, variant, number, name); ExportImage(kit, background, filter, Width, Height, variant, number, name); background.Delete(); }