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); }
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); }