Cleanup, before branching: Moved interop assemblies from the GreenshotInterop to this project

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2249 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-11-08 11:29:43 +00:00
commit 06c691cf89
14 changed files with 2829 additions and 36 deletions

View file

@ -0,0 +1,148 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Greenshot.Interop;
namespace Greenshot.Interop.Office {
public class WordExporter {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(WordExporter));
private static string version = null;
public static bool isAfter2003() {
if (version != null) {
return !version.StartsWith("11");
}
return false;
}
/// <summary>
/// Insert the bitmap stored under the tempfile path into the word document with the supplied caption
/// </summary>
/// <param name="wordCaption"></param>
/// <param name="tmpFile"></param>
/// <returns></returns>
public static bool InsertIntoExistingDocument(string wordCaption, string tmpFile) {
using (IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>()) {
if (wordApplication != null) {
for (int i = 1; i <= wordApplication.Documents.Count; i++) {
using (IWordDocument wordDocument = wordApplication.Documents.item(i)) {
if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) {
return InsertIntoExistingDocument(wordApplication, wordDocument, tmpFile);
}
}
}
}
}
return false;
}
/// <summary>
/// Internal method for the insert
/// </summary>
/// <param name="wordApplication"></param>
/// <param name="wordDocument"></param>
/// <param name="tmpFile"></param>
/// <returns></returns>
internal static bool InsertIntoExistingDocument(IWordApplication wordApplication, IWordDocument wordDocument, string tmpFile) {
if (wordApplication.Selection != null) {
AddPictureToSelection(wordApplication.Selection, tmpFile);
try {
wordDocument.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
} catch (Exception e) {
if (e.InnerException != null) {
LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message);
} else {
LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message);
}
}
try {
wordApplication.Activate();
} catch {}
try {
wordDocument.Activate();
} catch {}
try {
wordDocument.ActiveWindow.Activate();
} catch {}
return true;
}
return false;
}
private static void AddPictureToSelection(ISelection selection, string tmpFile) {
selection.InlineShapes.AddPicture(tmpFile, false, true, Type.Missing);
selection.InsertAfter("\r\n");
}
public static void InsertIntoNewDocument(string tmpFile) {
using (IWordApplication wordApplication = COMWrapper.GetOrCreateInstance<IWordApplication>()) {
if (wordApplication != null) {
wordApplication.Visible = true;
wordApplication.Activate();
// Create new Document
object template = string.Empty;
object newTemplate = false;
object documentType = 0;
object documentVisible = true;
IWordDocument wordDocument = wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible);
// Add Picture
AddPictureToSelection(wordApplication.Selection, tmpFile);
wordDocument.Activate();
wordDocument.ActiveWindow.Activate();
}
}
}
/// <summary>
/// Get the captions of all the open word documents
/// </summary>
/// <returns></returns>
public static List<string> GetWordDocuments() {
List<string> documents = new List<string>();
try {
using (IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>()) {
if (wordApplication != null) {
if (version == null) {
version = wordApplication.Version;
}
for (int i = 1; i <= wordApplication.Documents.Count; i++) {
IWordDocument document = wordApplication.Documents.item(i);
if (document.ReadOnly) {
continue;
}
if (isAfter2003()) {
if (document.Final) {
continue;
}
}
documents.Add(document.ActiveWindow.Caption);
}
}
}
} catch (Exception ex) {
LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
}
return documents;
}
}
}