using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
//----< using >----
//using Microsoft.Office.Tools.Ribbon; //*word ribbon
using Word = Microsoft.Office.Interop.Word; //*word document
using System.Windows.Forms; //*word OpenfileDialog
using Office = Microsoft.Office.Core; //*lock ratio width height
using Microsoft.Office.Interop.Word;
//----</ using >----
namespace Addin_Word_Photos_into_Table
{
public partial class Ribbon1
{
//< init >
Document _doc = null;
Word.Application _app = null;
//</ init >
#region Region: Ribbonbar
//===================< RibbonBar >===================
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
//tbxPhotos_Tablename.Text = Settings1.Default.Photos_Tablename;
}
//===================</ RibbonBar >===================
#endregion /Ribbonbar
#region --Button--
//===================< Buttons >===================
private void BtnPhotos_Insert_Click(object sender, RibbonControlEventArgs e)
{
_doc = Globals.ThisAddIn.Application.ActiveDocument;
_app = Globals.ThisAddIn.Application;
//< init setup >
string sInsert_Type = Settings1.Default.Insert_Type;
string sTargetname = Settings1.Default.Target_Label;
//</ init setup >
Word.Table table_Photos;
if(sInsert_Type.IndexOf("Nach Textzeile") >-1)
{
table_Photos = find_Textline_Create_Empty_Table(sTargetname);
}
else
{
table_Photos = find_Table_with_Header(sTargetname);
}
if (table_Photos != null)
{
insert_Photos_at_Table(table_Photos);
}
}
//}
private void btnSetup_Click(object sender, RibbonControlEventArgs e)
{
frmSetup frm = new frmSetup();
frm.Show();
}
//===================</ Buttons >===================
#endregion /Buttons
#region --Methods--
//===================< Methods >===================
private Word.Table find_Table_with_Header(string sHeader)
{
//-----------------< find_Table_with_Header() >-----------------
//--< find Table Header >--
//*Loop all tables and look for header
foreach (Word.Table table in _doc.Tables)
{
if (table.Rows.Count > 0)
{
int intColumns_Count = table.Columns.Count;
for (int iCol = 1; iCol <= intColumns_Count; iCol++)
{
//----< @Loop: Columns in Table_Header >----
Cell cell = null;
try
{
cell = table.Cell(1, iCol);
if (cell.Range.Text.IndexOf(sHeader) == 0)
{
return table;
}
}
catch (Exception)
{
//< endofcolumns >
break;
//</ endofcolumns >
}
//----</ @Loop: Columns in Table_Header >----
}
}
}
//< check >
System.Windows.Forms.MessageBox.Show("Tabelle mit Header nicht gefunden. " + Environment.NewLine +
"gesuchter Header:" + sHeader, "Header nicht gefunden");
return null;
//--</ find Table Header >--
//-----------------</ find_Table_with_Header() >-----------------
}
private Word.Table find_Textline_Create_Empty_Table(string sHeader)
{
//-----------------< find_Table_with_Header() >-----------------
//--< find Table Header >--
//*Loop all tables and look for header
foreach (Word.Range sLine in _doc.Sentences)
{
//-------< Check Sentence >------
string sText = sLine.Text;
//< check: Zeilenumbruch >
int posUmbruch = sText.IndexOf("\r");
if (posUmbruch>0)
{ sText = sText.Substring(0, posUmbruch); }
posUmbruch = sText.IndexOf("\n");
if (posUmbruch > 0)
{ sText = sText.Substring(0, posUmbruch); }
//</ check: Zeilenumbruch >
sText = sText.Trim();
if (sText == sHeader )
{
//----< Line_found >----
sLine.End= sLine.Start+sText.Length;
sLine.Select();
_app.Selection.Select();
_app.Selection.EndKey();
sLine.InsertAfter("\n\r");
_app.Selection.MoveDown();
_app.Selection.MoveDown();
Range newRange = _app.Selection.Range;
_app.ScreenRefresh();
//< init setup >
int Columns_Count = Settings1.Default.Columns_Count;
//</ init setup >
//< create new table >
Word.Table newTable = _doc.Tables.Add( newRange ,NumRows:1,NumColumns: Columns_Count);
newTable.Range.Font.Name = "Arial";
newTable.Range.Font.Size=11;
newTable.Range.Font.Bold = 0;
newTable.Range.Font.Underline = 0;
//</ create new table >
//< out >
return newTable;
//</ out >
//----</ Line_found >----
}
//-------</ Check Sentence >------
}
//< check >
System.Windows.Forms.MessageBox.Show("Zeile nicht gefunden. " + Environment.NewLine + "gesuchter Header:" + sHeader,
"Header nicht gefunden");
return null;
//--</ find Table Header >--
//-----------------</ find_Table_with_Header() >-----------------
}
private void insert_Photos_at_Table(Word.Table table_Photos)
{
//-----------------< insert_Photos_by_TableHeader() >-----------------
//< init >
int intColumns_count = table_Photos.Columns.Count;
//</ init >
//--< Word-FileDialog >--
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "image files (*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff";
fileDialog.Multiselect = true;
//fileDialog.InitialDirectory = "B:\\2016";
var result = fileDialog.ShowDialog();
//--</ Word-FileDialog >--
//< check >
//*no file
if (result != System.Windows.Forms.DialogResult.OK)
{ return; }
//</ check >
//< init setup >
bool With_Filename = Settings1.Default.Show_Filename;
bool With_Empty_Line = Settings1.Default.With_Empty_Line;
int Columns_count = Settings1.Default.Columns_Count;
string sInsert_Type = Settings1.Default.Insert_Type;
//</ init setup >
//--< check exits Entries >--
try
{
Row row = table_Photos.Rows.Last;
if (row.Range.InlineShapes.Count > 0)
{
table_Photos.Rows.Add();
}
}
catch (Exception)
{
}
//--</ check exits Entries >--
//-------< @Loop: Insert all Images >--------
int iCol = 0;
foreach (String sfileName in fileDialog.FileNames)
{
//------< Loop.Item >------
iCol++;
if (iCol > 2)
{
iCol = 1;
table_Photos.Rows.Add();
}
int iRow = table_Photos.Rows.Count;
Cell cell = table_Photos.Cell(iRow, iCol);
float image_Width = cell.Width - cell.RightPadding - cell.LeftPadding;
//< get selection >
Range act_Image_Range = cell.Range;
//</ get selection >
//----< Insert Image >----
//< insert picture from Link >
Word.InlineShape inlineShape = _doc.InlineShapes.AddPicture(sfileName, LinkToFile: true,
SaveWithDocument: true, Range: act_Image_Range);
//</ insert picture from Link >
//< scale >
inlineShape.LockAspectRatio = Office.MsoTriState.msoTrue;
inlineShape.Width = image_Width;
//</ scale >
//--< replace as png >--
//*reduce memory 1 MB to 1kb
//< cut >
inlineShape.Select();
_app.Selection.Cut();
//</ cut >
//*pasteBitmap is much smaller
act_Image_Range.PasteSpecial(Link: false, DataType: Word.WdPasteDataType.wdPasteBitmap,
Placement: Word.WdOLEPlacement.wdInLine, DisplayAsIcon: false);
act_Image_Range.Select();
//--</ replace as png >--
//--< Filename >--
if (With_Filename)
{
int posFilename = sfileName.LastIndexOf("\\");
if(posFilename>-1)
{
string sFileName_Short = sfileName.Substring(posFilename+1);
_app.Selection.EndKey();
_app.Selection.Font.Name = "Arial";
_app.Selection.Font.Size = 11;
_app.Selection.Font.Bold = 0;
_app.Selection.Font.Underline = 0;
_app.Selection.TypeParagraph();
_app.Selection.TypeText(sFileName_Short);
}
}
//--</ Filename >--
//--< Empty_Line >--
if (With_Empty_Line)
{
_app.Selection.EndKey();
_app.Selection.TypeParagraph();
}
//--</ Empty_Line >--
_app.ScreenRefresh();
}
//-------</ @Loop: Insert all Images >--------
//-----------------</ insert_Photos_at_Table_with_Header() >-----------------
}
//===================</ Methods >===================
#endregion /Methods
}
}
|