public static void RemoveTransparentPartsFromImages(string[] ImagePaths, ILittleLyreLogger Logger) { try { // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Operation", LogMessage = "Trying to remove transparent parts from images...", LogSeverity = LoggerParameters.LogSeverity.INFO }); if (ImagePaths == null) { // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Operation", LogMessage = "Input is [null]. Process cancelled.", LogSeverity = LoggerParameters.LogSeverity.INFO }); return; } int CropY1 = 0, CropX1 = 0, CropY2 = 0, CropX2 = 0; bool RFound = false; Bitmap mybmp = null; // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Operation", LogMessage = "Image path count: " + ImagePaths.Length, LogSeverity = LoggerParameters.LogSeverity.INFO }); for (int li = 0; li < ImagePaths.Length; li++) { mybmp = new Bitmap(ImagePaths[li], true); CropY1 = 0; CropX1 = 0; CropY2 = mybmp.Height; CropX2 = mybmp.Width; // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Operation", LogMessage = "Image [" + (li + 1) + "/" + ImagePaths.Length + "] Size -> [" + CropX2 + " : " + CropY2 + "] Path -> [ " + ImagePaths[li] + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); //Log info //AddToLog("Intial image [width, height] = " + mybmp.Width + ", " + mybmp.Height); RFound = false; for (int h = 0; h < mybmp.Height; h++) { for (int w = 0; w < mybmp.Width; w++) { if (mybmp.GetPixel(w, h).A != 0) { RFound = true; break; } } if (RFound == false) { CropY1 = h; } else { break; } } RFound = false; for (int h = (mybmp.Height - 1); h >= 0; h--) { for (int w = 0; w < mybmp.Width; w++) { if (mybmp.GetPixel(w, h).A != 0) { RFound = true; break; } } if (RFound == false) { CropY2 = h; } else { break; } } RFound = false; for (int w = (mybmp.Width - 1); w >= 0; w--) { for (int h = 0; h < mybmp.Height; h++) { if (mybmp.GetPixel(w, h).A != 0) { RFound = true; break; } } if (RFound == false) { CropX2 = w; } else { break; } } RFound = false; for (int w = 0; w < mybmp.Width; w++) { for (int h = 0; h < mybmp.Height; h++) { if (mybmp.GetPixel(w, h).A != 0) { RFound = true; break; } } if (RFound == false) { CropX1 = w; } else { break; } } mybmp.Dispose(); mybmp = new Bitmap(CropX2 - CropX1, CropY2 - CropY1); Graphics Grp = Graphics.FromImage(mybmp); Grp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic; Grp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; Image TImg = Image.FromFile(ImagePaths[li]); Grp.DrawImage(TImg, new RectangleF(0, 0, mybmp.Width, mybmp.Height), new RectangleF(CropX1, CropY1, CropX2 - CropX1, CropY2 - CropY1), GraphicsUnit.Pixel); Grp.Dispose(); TImg.Dispose(); mybmp.Save(ImagePaths[li], ImageFormat.Png); // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Operation", LogMessage = "Image [" + (li + 1) + "/" + ImagePaths.Length + "] cropped and saved. New Size -> [" + mybmp.Width + " : " + mybmp.Height + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); } } catch (Exception Exp) { // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Operation", LogMessage = "Error occured during operation. Error message -> [" + Exp.Message + "]", LogSeverity = LoggerParameters.LogSeverity.ERROR }); } }
public static WTable[] GetAllTables(Application MWordApp, Document MWordDocument, ILittleLyreLogger Logger) { Document DraftDoc = MWordApp.Documents.Add(); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); List <WTable> TList = new List <WTable>(); WCSelection[] TSelection = GetAllContentSelections(MWordApp, MWordDocument, Logger); { WCSelection[] ListSelections = WListReader.GetAllContentSelections(MWordApp, MWordDocument, Logger); TSelection = WCSelectionOperations.RemoveChilds(TSelection, ListSelections, Logger); } for (int tbls = 0; tbls < TSelection.Length; tbls++) { //// Log info //AddToLog("Working on table: " + tbli); //// Log info //AddToLog("Getting table content. Row count: " + CurTable.Rows.Count + "; Column count: " + CurTable.Columns.Count); string TableXML = MWordDocument.Range(TSelection[tbls].ContentSelectionStart, TSelection[tbls].ContentSelectionEnd).XML; WTable wtbl = GetTableFromTableXMLData(TableXML); //for (int trow = 1; trow <= CurTable.Rows.Count; trow++) //{ // string[] TRow = new string[CurTable.Columns.Count]; // for(int tcol = 1; tcol <= CurTable.Columns.Count; tcol++) // { // try // { // TRow[tcol - 1] = CurTable.Cell(trow, tcol).Range.Text; // } // catch // { // TRow[tcol - 1] = string.Empty; // } // } // TList_Tables.Add(TRow); //} //// Log info wtbl.ContentSelection = TSelection[tbls]; TList.Add(wtbl); //// Log info //AddToLog("Table was added as selection. Selection[Start, End] = [" + cs.RStart + ", " + cs.REnd + "]"); } DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); return(TList.ToArray()); }
public static WCSelection[] GetAllContentSelections(Application MWordApp, Document MWordDocument, ILittleLyreLogger Logger) { Document DraftDoc = MWordApp.Documents.Add(Visible: false); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); List <WCSelection> TableSelections = new List <WCSelection>(); for (int tbli = 1; tbli <= DraftDoc.Tables.Count; tbli++) { //// Log info //AddToLog("Working on table: " + tbli); Table CurTable = DraftDoc.Tables[tbli]; //string TableXML = CurTable.Range.XML; //// Log info //AddToLog("Getting table content. Row count: " + CurTable.Rows.Count + "; Column count: " + CurTable.Columns.Count); //WTable wtbl = GetTableFromTableXMLData(TableXML); //for (int trow = 1; trow <= CurTable.Rows.Count; trow++) //{ // string[] TRow = new string[CurTable.Columns.Count]; // for(int tcol = 1; tcol <= CurTable.Columns.Count; tcol++) // { // try // { // TRow[tcol - 1] = CurTable.Cell(trow, tcol).Range.Text; // } // catch // { // TRow[tcol - 1] = string.Empty; // } // } // TList_Tables.Add(TRow); //} //// Log info //AddToLog("All content collected..."); CurTable.Select(); WCSelection wcs = new WCSelection() { ContentSelectionStart = MWordApp.Selection.Start, ContentSelectionEnd = MWordApp.Selection.End }; TableSelections.Add(wcs); //// Log info //AddToLog("Table was added as selection. Selection[Start, End] = [" + cs.RStart + ", " + cs.REnd + "]"); } TableSelections = new List <WCSelection>(WCSelectionOperations.RemoveChilds(TableSelections.ToArray())); //{ // WCSelection[] ListSelections = WListReader.GetAllContentSelections(MWordApp, MWordDocument, Logger); // TableSelections = new List<WCSelection>(WCSelectionOperations.RemoveChilds(TableSelections.ToArray(), ListSelections, Logger)); //} MWordDocument.Activate(); DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); { for (int ls = 0; ls < TableSelections.Count; ls++) { TableSelections[ls].ContentID = "WTable_" + (ls + 1); } } return(TableSelections.ToArray()); }
public static WParagraph[] GetAllParagraphs(Application MWordApp, Document MWordDocument, ILittleLyreLogger Logger) { List <WParagraph> ListParagraphs = new List <WParagraph>(); Document DraftDoc = MWordApp.Documents.Add(Visible: false); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); //// Log info //AddToLog("Trying to get all paragraphs..."); //if (MyDraftDoc.Paragraphs.Count < 1) //{ // AddToLog("No paragraphs found. Going to next step..."); //} //else //{ // AddToLog("Paragraph count: " + MyDraftDoc.Paragraphs.Count); //} // Log Logger.AddLog(new LogContent() { LogSubject = "Selection Array", LogMessage = "Getting first level Paragraph selections...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); WCSelection[] AllPSelecs = GetAllContentSelections(MWordApp, MWordDocument, Logger); // Log Logger.AddLog(new LogContent() { LogSubject = "Selection Array", LogMessage = "Getting all first level table selections...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); WCSelection[] TableWCS = WTableReader.GetAllContentSelections(MWordApp, MWordDocument, Logger); // Log Logger.AddLog(new LogContent() { LogSubject = "Selection Array", LogMessage = "Getting all first level list selections...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); WCSelection[] ListWCS = WListReader.GetAllContentSelections(MWordApp, MWordDocument, Logger); // Log Logger.AddLog(new LogContent() { LogSubject = "Selection Array", LogMessage = "Getting all Table Of Content selections...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); WCSelection[] TOCWCS = WAElement.GetAllContentSelections(MWordApp, MWordDocument, Logger); //// Log //Logger.AddLog(new LogContent() { LogSubject = "Selection Array", LogMessage = "Getting all Image selections...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); //WCSelection[] ImageSelections = WImage.GetAllContentSelections(MWordApp, MWordDocument, Logger); // Log Logger.AddLog(new LogContent() { LogSubject = "Arranging Selection", LogMessage = "Comparing to a new arranged array by using table and list content selections...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); WCSelection[] AllPrgCSelections = WCSelectionOperations.RemoveCompairingParts(AllPSelecs, WCSelectionOperations.CreateNewArrangedSelectionArray(TableWCS, ListWCS)); // Log Logger.AddLog(new LogContent() { LogSubject = "Arranging Selection", LogMessage = "Comparing with image selection...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); AllPrgCSelections = WCSelectionOperations.RemoveCompairingParts(AllPrgCSelections, TOCWCS); //AllPrgCSelections = WCSelectionOperations.RemoveAdditonalCompairingParts(AllPrgCSelections, TOCWCS, Logger); XmlDocument DraftXMLDoc = new XmlDocument(); for (int psel = 0; psel < AllPrgCSelections.Length; psel++) { WParagraph wparag = new WParagraph(); wparag.ContentSelection = AllPrgCSelections[psel]; try { //Range PRange = MWordDocument.Range(AllPrgCSelections[psel].ContentSelectionStart, AllPrgCSelections[psel].ContentSelectionEnd); //string NLocal = PRange.get_Style().NameLocal; string ParagXML = MWordDocument.Range(AllPrgCSelections[psel].ContentSelectionStart, AllPrgCSelections[psel].ContentSelectionEnd).XML; DraftXMLDoc.LoadXml(ParagXML); string tmpBody = DraftXMLDoc.GetElementsByTagName(WordXMLTags.WTN_Body)[0].OuterXml; DraftXMLDoc.RemoveAll(); DraftXMLDoc.LoadXml(tmpBody); XmlNode PrgNd = DraftXMLDoc.GetElementsByTagName(WordXMLTags.WordTagName_Paragraph)[0]; wparag = GetParagraphFromParagraphXMLNode(PrgNd); wparag.ContentSelection = AllPrgCSelections[psel]; ListParagraphs.Add(wparag); DraftXMLDoc.RemoveAll(); // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Paragraph", LogMessage = "Paragraph [" + (psel + 1) + "/" + AllPrgCSelections.Length + "] added. Content selection -> [" + AllPrgCSelections[psel].ContentSelectionStart + " : " + AllPrgCSelections[psel].ContentSelectionEnd + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); } catch (Exception Exp) { // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Paragraph", LogMessage = "Error occured. Error message -> [" + Exp.Message + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); //ListParagraphs.Add(wparag); //// Log //Logger.AddLog(new LogContent() { LogSubject = "Getting Paragraph", LogMessage = "Paragraph [" + (psel + 1) + "/" + "] added. Content selection -> [" + AllPrgCSelections[psel].ContentSelectionStart + " : " + AllPrgCSelections[psel].ContentSelectionEnd + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); } } //for (int prg = 1; prg <= DraftDoc.Paragraphs.Count; prg++) //{ // //MyStringBuilder.Append(MyDraftDoc.Paragraphs[prg].Range.Text + Environment.NewLine); // WCSelection wcs = new WCSelection(); // WParagraph wparag = new WParagraph(); // WParagraphProperties wparagp = new WParagraphProperties(); // //wparag.ParagraphProperties = wparagp; // wcs.ContentID = "Paragraph_" + prg; // wcs.ContentSelectionStart = DraftDoc.Paragraphs[prg].Range.Start; // wcs.ContentSelectionEnd = DraftDoc.Paragraphs[prg].Range.End; // XmlDocument DraftXMLDoc = new XmlDocument(); // try // { // { // //if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading1].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading1; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading2].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading2; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading3].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading3; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading4].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading4; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading5].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading5; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading6].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading6; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading7].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading7; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading8].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading8; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleHeading9].NameLocal) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading9; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal.ToString().ToLower.Contains("heading")) // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordHeading9; // //} // //else if (DraftDoc.Paragraphs[prg].Range.get_Style().NameLocal == DraftDoc.Styles[WdBuiltinStyle.wdStyleListParagraph].NameLocal) // //{ // // break; // //} // //else // //{ // // wparagp.ParagraphStyle = ParagraphStyle.WordSimpleParagraph; // //} // //if (DraftDoc.Paragraphs[prg].Alignment == WdParagraphAlignment.wdAlignParagraphCenter) // //{ // // wparagp.Alingment = Alignment.Center; // //} // //else if (DraftDoc.Paragraphs[prg].Alignment == WdParagraphAlignment.wdAlignParagraphLeft) // //{ // // wparagp.Alingment = Alignment.Left; // //} // //else if (DraftDoc.Paragraphs[prg].Alignment == WdParagraphAlignment.wdAlignParagraphRight) // //{ // // wparagp.Alingment = Alignment.Right; // //} // //else // //{ // // wparagp.Alingment = Alignment.Both; // //} // } // string ParagXML = DraftDoc.Paragraphs[prg].Range.XML; // DraftXMLDoc.LoadXml(ParagXML); // XmlNode PrgNd = DraftXMLDoc.GetElementsByTagName(WordXMLTags.WordTagName_Paragraph)[0]; // wparag = GetParagraphFromParagraphXMLNode(PrgNd); // wparag.ParagraphProperties.ContentSelection = wcs; // ListParagraphs.Add(wparag); // DraftXMLDoc.RemoveAll(); // } // catch // { // wparagp.ContentSelection = wcs; // wparag.ParagraphProperties = wparagp; // ListParagraphs.Add(wparag); // DraftXMLDoc.RemoveAll(); // } // //// Log info // //AddToLog("Paragraph [" + prg + "/" + MyDraftDoc.Paragraphs.Count + "] -> Selection[Start, End] = " + "[" + Selcs[0] + ", " + Selcs[1] + "]"); //} MWordDocument.Activate(); DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); return(ListParagraphs.ToArray()); }
public static WCSelection[] GetAllContentSelections(Application MWordApp, Document MWordDocument, ILittleLyreLogger Logger) { try { // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Elements", LogMessage = "Trying to get all paragraphs." }); // Log Logger.AddLog(new LogContent() { LogSubject = "Creating Document", LogMessage = "Trying to create draft document...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); Document DraftDoc = MWordApp.Documents.Add(Visible: false); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); // Log Logger.AddLog(new LogContent() { LogSubject = "Creating Document", LogMessage = "Draft document created.", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); List <WCSelection> ParagWCS = new List <WCSelection>(); // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "Trying to get all first level paragraph content selections" }); for (int prg = 1; prg <= DraftDoc.Paragraphs.Count; prg++) { //MyStringBuilder.Append(MyDraftDoc.Paragraphs[prg].Range.Text + Environment.NewLine); WCSelection wcs = new WCSelection(); //wparag.ParagraphProperties = wparagp; wcs.ContentID = "DParagraph_" + prg; wcs.ContentSelectionStart = DraftDoc.Paragraphs[prg].Range.Start; wcs.ContentSelectionEnd = DraftDoc.Paragraphs[prg].Range.End; // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "Paragraph [" + prg + "/" + DraftDoc.Paragraphs.Count + "] added. Content selection -> [" + wcs.ContentSelectionStart + " : " + wcs.ContentSelectionEnd + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); ParagWCS.Add(wcs); } // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "All paragraph content selection collected." }); //Thread.Sleep(500); WCSelection[] NParagWCS = WCSelectionOperations.RemoveChilds(ParagWCS.ToArray()); for (int pindx = 0; pindx < NParagWCS.Length; pindx++) { NParagWCS[pindx].ContentID = "WParagraph_" + (pindx + 1); } DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); return(NParagWCS); } catch (Exception Exp) { // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "Error occured. Message -> " + Exp.Message }); return(null); } }
public static WCSelection[] RemoveChilds(WCSelection[] MainWCSelections, WCSelection[] WCSelectionsToCompare, ILittleLyreLogger Logger) { if (WCSelectionsToCompare == null) { return(MainWCSelections); } if (MainWCSelections == null) { return(null); } List <WCSelection> ListWCS = new List <WCSelection>(MainWCSelections); int _Lindex = 0; int _TIndex = 0; int _LCount = ListWCS.Count; int _LPos = 0; while (_Lindex < _LCount) { _TIndex = _LPos; while (_TIndex < WCSelectionsToCompare.Length) { if (ListWCS[_Lindex].ContentSelectionStart >= WCSelectionsToCompare[_TIndex].ContentSelectionStart && ListWCS[_Lindex].ContentSelectionEnd <= WCSelectionsToCompare[_TIndex].ContentSelectionEnd) { ListWCS.RemoveAt(_Lindex); _LCount = ListWCS.Count; _LPos = _TIndex; break; } _TIndex++; } _Lindex++; } return(ListWCS.ToArray()); }
public static WImage[] ExportImages(Application MWordApp, Document MWordDocument, string OutputFolder, string ImageFileName, ILittleLyreLogger Logger) { //// Log info //AddToLog("Trying to get inline shapes...)"); //if (MyDraftDoc.InlineShapes.Count < 1) //{ // AddToLog("There is no inline shapes in the word document."); //} //else //{ // AddToLog("Inline shapes count: " + MyDraftDoc.InlineShapes.Count); //} try { // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Exporting", LogMessage = "Trying get all image content selections...", LogSeverity = LoggerParameters.LogSeverity.INFO }); WCSelection[] ImageWCSList; ImageWCSList = WImage.GetAllContentSelections(MWordApp, MWordDocument, Logger); List <WImage> ImageList = new List <WImage>(); for (int wcsl = 0; wcsl < ImageWCSList.Length; wcsl++) { WImage wimg = new WImage(); wimg.ImagePath = OutputFolder + "\\" + ImageFileName + "_" + ImageWCSList[wcsl].ContentID + ".png"; wimg.ContentSelection = ImageWCSList[wcsl]; ImageList.Add(wimg); } Document DraftDoc = MWordApp.Documents.Add(Visible: false); //Document DraftDoc = WordApp.Documents.Add(WParameters.Missing, WParameters.Missing, WParameters.Missing, false); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); { // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Exporting", LogMessage = "Trying to save each image content selection data as image file.", LogSeverity = LoggerParameters.LogSeverity.INFO }); DirectoryInfo OutpFol = new DirectoryInfo(OutputFolder); if (OutpFol.Exists == false) { OutpFol.Create(); } for (int isel = 0; isel < ImageList.Count; isel++) { //MWordDocument.Range(ImageList[isel].ContentSelection.ContentSelectionStart, ImageList[isel].ContentSelection.ContentSelectionEnd).Select(); MWordApp.Selection.Start = ImageList[isel].ContentSelection.ContentSelectionStart; MWordApp.Selection.End = ImageList[isel].ContentSelection.ContentSelectionEnd; byte[] ImgData = MWordApp.Selection.Range.EnhMetaFileBits; MemoryStream TMStream = new MemoryStream(ImgData); Image ImgFS = Image.FromStream(TMStream); ImgFS.Save(ImageList[isel].ImagePath, ImageFormat.Png); ImgFS.Dispose(); // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Exporting", LogMessage = "Image content selection -> [" + ImageList[isel].ContentSelection.ContentSelectionStart + " : " + ImageList[isel].ContentSelection.ContentSelectionEnd + "] was saved as image. Path -> [" + ImageList[isel].ImagePath + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); } } // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Exporting", LogMessage = "Preparing images to be cropped -> [Getting all paths]", LogSeverity = LoggerParameters.LogSeverity.INFO }); string[] ImgPaths = ImageList.Select(a => a.ImagePath).ToArray(); ImageOperations.RemoveTransparentPartsFromImages(ImgPaths, Logger); // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Exporting", LogMessage = "All images was cropped.", LogSeverity = LoggerParameters.LogSeverity.INFO }); DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); return(ImageList.ToArray()); } catch (Exception Exp) { // Logging Logger.AddLog(new LogContent() { LogSubject = "Image Exporting", LogMessage = "Error occured during operation. Error message -> [" + Exp.Message + "]", LogSeverity = LoggerParameters.LogSeverity.ERROR }); return(null); } }
private void frmDokuWikiFC_Load(object sender, EventArgs e) { // Log rtxtLogControl.Text += "-> [Initializing]" + " Trying to load configurations..." + Environment.NewLine; ConfigBuilder CBld = new ConfigBuilder(); try { object Conf = CBld.BuildConfig(MyConfFolder + "\\" + MyConfFileName); if (Conf == null) { // Log rtxtLogControl.Text += "-> [Initializing]" + " Something went wront during loading configuration. Check configuration file." + Environment.NewLine; CBld = new ConfigBuilder(); // Log rtxtLogControl.Text += "-> [Initializing]" + " Loading default configuration..." + Environment.NewLine; } MyWorkingFolder = CBld.Directories_WorkingDirectory; MyOutputFolder = CBld.Directories_OutputDirectory; // Log rtxtLogControl.Text += "-> [Initializing]" + " Configuration loaded." + Environment.NewLine; } catch (Exception Exp) { // Log rtxtLogControl.Text += "-> [Initializing]" + " Error occured during loading configuration(check confiuration file). Message -> " + Exp.Message + Environment.NewLine; // Log rtxtLogControl.Text += "-> [Initializing]" + " Loading default configuration..." + Environment.NewLine; CBld = new ConfigBuilder(); MyWorkingFolder = CBld.Directories_WorkingDirectory; MyOutputFolder = CBld.Directories_OutputDirectory; // Log rtxtLogControl.Text += "-> [Initializing]" + "Configuration loaded." + Environment.NewLine; } MyLogger = new LineLogger(); MyLogger.LogLineCount = CBld.SHLogger_LogLineCount; LogLCount = CBld.SHLogger_LogLineCount; MyLogger.LogLineStartString = CBld.SHLogger_LogLineStartString; MyLogger.LogSubjectLength = CBld.SHLogger_LogSubjetLength; MyLogger.LogMessageLength = CBld.SHLogger_LogMessageLength; MyLogger.OnLogAdded += GetLog; DWFormatter = new DWikiFormatter(MyOutputFolder, MyWorkingFolder); DWFormatter.ExportTOC = CBld.DWikiExporter_ExportTOC; DWFormatter.ExportTOF = CBld.DWikiExporter_ExportTOF; DWFormatter.AddFooterInfo = CBld.DWikiExporter_AddFooterInfo; DWFormatter.ListOrder = CBld.DWikiExporter_ListOrder; DWFormatter.ShiftOrdering = CBld.DWikiExporter_ShiftOrdering; DWFormatter.TableFirstRowIsHeader = CBld.DokuWikiExporter_TableFirstRowIsHeader; DWFormatter.ConsiderImageSize = CBld.DokuWikiExporter_ConsiderImageSize; SHMicroMWordLib_JoinImageSelections = CBld.SHMicroMWordLib_JoinImageSelections; CheckDirectories(); CheckForIllegalCrossThreadCalls = false; ArrangeForm(); ListOfFiles = new List <string>(); btnProcessStart.Enabled = false; btnAddFile.Enabled = true; rtxtLogProcess.BackColor = Color.Black; rtxtLogProcess.ForeColor = Color.Lime; rtxtLogProcess.ReadOnly = true; //MyWiki.OnLogAdded += GetLog; // Log rtxtLogControl.Text += "-> [Initializing]" + " Be sure You have finished and saved your work in 'Microsoft Word'. With starting of exporting process all 'Microsoft Word' documents will be closed immediately." + Environment.NewLine + Environment.NewLine; }
public static WList[] GetAllLists(Application MWordApp, Document MWordDocument, ILittleLyreLogger Logger) { // Log Logger.AddLog(new LogContent() { LogSubject = "Getting List", LogMessage = "Getting all first level list elements..." }); Document DraftDoc = MWordApp.Documents.Add(Visible: false); //Document DraftDoc = MWordApp.Documents.Add(null, null, null, false); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); List <WList> AllLists = new List <WList>(); WCSelection[] ListSelection = GetAllContentSelections(MWordApp, MWordDocument, Logger); // Log Logger.AddLog(new LogContent() { LogSubject = "Getting List", LogMessage = "All list content selections collected.", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); { WCSelection[] TableSelections = WTableReader.GetAllContentSelections(MWordApp, MWordDocument, Logger); ListSelection = WCSelectionOperations.RemoveChilds(ListSelection, TableSelections, Logger); // Log Logger.AddLog(new LogContent() { LogSubject = "Getting List", LogMessage = "Child elements removed against table selection.", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); } // Log Logger.AddLog(new LogContent() { LogSubject = "Getting List", LogMessage = "Getting and arranging all first level list contents ...", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); { int _csIndex = 0; int _lsCount = ListSelection.Length; while (_csIndex < _lsCount) { string ListXML = null; try { ListXML = MWordDocument.Range(ListSelection[_csIndex].ContentSelectionStart, ListSelection[_csIndex].ContentSelectionEnd).XML; if (string.IsNullOrEmpty(ListXML) == true) { _csIndex++; continue; } } catch { _csIndex++; continue; } XmlDocument DrftDocFullCntx = new XmlDocument(); DrftDocFullCntx.LoadXml(ListXML); WList LContent = GetListFromListXMLData((DrftDocFullCntx.GetElementsByTagName(WordXMLTags.WTN_Body)[0]).ChildNodes[0]); LContent.ContentSelection = ListSelection[_csIndex]; AllLists.Add(LContent); _csIndex++; } } // Log Logger.AddLog(new LogContent() { LogSubject = "Getting List", LogMessage = "All list contents collected and arranged.", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); MWordDocument.Activate(); DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); return(AllLists.ToArray()); }
public static WCSelection[] GetAllContentSelections(Application MWordApp, Document MWordDocument, ILittleLyreLogger Logger) { // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "Trying to get all list content selections..." }); Document DraftDoc = MWordApp.Documents.Add(Visible: false); //Document DraftDoc = MWordApp.Documents.Add(null, null, null, false); MWordDocument.Select(); MWordApp.Selection.Copy(); DraftDoc.Range().Paste(); DraftDoc.Activate(); List <WCSelection> ListSelection = new List <WCSelection>(); for (int wlist = 1; wlist <= DraftDoc.Lists.Count; wlist++) { List dlist = DraftDoc.Lists[wlist]; dlist.Range.Select(); WCSelection wcs = new WCSelection() { ContentSelectionStart = MWordApp.Selection.Start, ContentSelectionEnd = MWordApp.Selection.End }; ListSelection.Add(wcs); // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "List [" + wlist + "/" + DraftDoc.Lists.Count + "] added. Content selection [start : end] = [" + wcs.ContentSelectionStart + " : " + wcs.ContentSelectionEnd + "]", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); //// Log info //AddToLog("List [" + wlist + "/" + MyDraftDoc.Lists.Count + "] has been added as selection. Selection[Start, End] = [" + cs.RStart + ", " + cs.REnd + "]"); } ListSelection = new List <WCSelection>(WCSelectionOperations.RemoveChilds(ListSelection.ToArray())); // Log Logger.AddLog(new LogContent() { LogSubject = "Getting Selection", LogMessage = "Child content selections removed.", LogSeverity = LoggerParameters.LogSeverity.DEBUG }); //{ // WCSelection[] TableSelections = WTableReader.GetAllContentSelections(MWordApp, MWordDocument, Logger); // ListSelection = new List<WCSelection>(WCSelectionOperations.RemoveChilds(ListSelection.ToArray(), TableSelections, Logger)); //} { for (int ls = 0; ls < ListSelection.Count; ls++) { ListSelection[ls].ContentID = "WList_" + (ls + 1); } } MWordDocument.Activate(); DraftDoc.Close(WdSaveOptions.wdDoNotSaveChanges, WParameters.Missing, WParameters.Missing); return(ListSelection.ToArray()); }