SourceCode/PROMS/LBWordLibrary/LBObjectExtension.cs
2009-12-07 15:52:32 +00:00

288 lines
9.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Win32;
using System.Windows.Forms;
namespace LBWordLibrary
{
public partial class LBApplicationClass : LBComObject
{
/// <summary>
/// This gives the option of specifying Background = false to print in the foreground.
/// </summary>
/// <param name="Background">boolean BackGround
/// If true - document will be printed in the background
/// If false - document will be printed in the foreground</param>
public void PrintOut(Boolean Background)
{
InvokeMethod("PrintOut", Background, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
public bool VolianPDFInstalled
{
get
{
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
if (printer == "VolianPDF Writer") return true;
return false;
}
}
public bool WordPDFExporterInstalled
{
get
{
// this is for ExportAsFixedFormat MSOffice AddIn
RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"Installer\Components\12B306B24E250DD428FC7016B6FB4BD8");
if (key != null)
{
key.Close();
return true;
}
return false;
}
}
private bool CanCreatePDFFile(string pdfFileName)
{
System.IO.FileInfo file = new System.IO.FileInfo(pdfFileName);
if (!file.Exists) return true;
try
{
file.Delete();
return true;
}
catch (System.IO.IOException exio)
{
if (exio.Message.StartsWith("The process cannot access the file")) return false;
throw new Exception(string.Format("Cannot Delete PDF file {0}", pdfFileName), exio);
}
catch (Exception ex)
{
throw new Exception(string.Format("Cannot Delete PDF file {0}", pdfFileName), ex);
}
}
private string AvailableFileName(string pdfFileName)
{
if (CanCreatePDFFile(pdfFileName)) return pdfFileName;
string prefix = pdfFileName.Replace(".pdf", "");
for (int i = 1; i < 1000; i++)
{
string newname = string.Format("{0}_{1:000}.PDF", prefix, i);
if (CanCreatePDFFile(newname)) return newname;
}
throw new Exception("Cannot find a name to use");
}
public string CreatePDF(string pdfFileName, bool openPDF)
{
pdfFileName = CreatePDF(pdfFileName);
if (openPDF) OpenPDF(pdfFileName);
return pdfFileName;
}
static List<System.Diagnostics.Process> _AcrobatProcesses=new List<System.Diagnostics.Process>();
private static void OpenPDF(string pdfFileName)
{
_AcrobatProcesses.Add(System.Diagnostics.Process.Start(pdfFileName));
}
public static void ClosePDFs()
{
foreach(System.Diagnostics.Process proc in _AcrobatProcesses)
if (!proc.HasExited)
KillAndWait(proc);
}
private static void KillAndWait(System.Diagnostics.Process proc)
{
Console.WriteLine("{0:s.ffff} Killing Adobe", DateTime.Now);
DateTime tStart = DateTime.Now;
proc.Kill();
DateTime tEnd = DateTime.Now.AddMilliseconds(100);
while (DateTime.Now < tEnd)
{
Application.DoEvents();
}
Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss.ffff} {1:yyyy-MM-dd HH:mm:ss.ffff} {2}", DateTime.Now, proc.ExitTime
,TimeSpan.FromTicks(proc.ExitTime.Ticks - tStart.Ticks).TotalMilliseconds);
}
public string CreatePDF(string pdfFileName)
{
pdfFileName = AvailableFileName(pdfFileName);
if (Convert.ToSingle(Version) >= 12.0F && WordPDFExporterInstalled)
return CreatePDF2007(pdfFileName);
else if (VolianPDFInstalled)
return CreatePDF2003BG(pdfFileName);
else
throw new Exception("No PDF Writer support installed for MS Word sections");
}
public string CreatePDF2003FG(string pdfFileName, bool openPDF)
{
pdfFileName = CreatePDF2003FG(pdfFileName);
if (openPDF) OpenPDF(pdfFileName);
return pdfFileName;
}
public string CreatePDF2003FG(string pdfFileName)
{
pdfFileName = AvailableFileName(pdfFileName);
if (!VolianPDFInstalled)
throw new Exception("VolianPDF Writer is not installed properly");
try
{
string printer = ActivePrinter;
RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\VolianPDF Writer");
key.SetValue("OutputFile", pdfFileName, RegistryValueKind.String);
key.SetValue("BypassSaveAs", "1", RegistryValueKind.String);
key.Close();
ActivePrinter = "VolianPDF Writer";
PrintOut(false);
ActivePrinter = printer;
Registry.CurrentUser.DeleteSubKeyTree(@"Software\VolianPDF Writer");
return pdfFileName;
}
catch (Exception ex)
{
throw new Exception("Error creating PDF - LBApplicationClass.CreatePDF2003FG", ex);
}
}
public string CreatePDF2003BG(string pdfFileName, bool openPDF)
{
pdfFileName = CreatePDF2003BG(pdfFileName);
if (openPDF) OpenPDF(pdfFileName);
return pdfFileName;
}
public string CreatePDF2003BG(string pdfFileName)
{
pdfFileName = AvailableFileName(pdfFileName);
if (!VolianPDFInstalled)
throw new Exception("VolianPDF Writer is not installed properly");
try
{
string printer = ActivePrinter;
RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\VolianPDF Writer");
key.SetValue("OutputFile", pdfFileName, RegistryValueKind.String);
key.SetValue("BypassSaveAs", "1", RegistryValueKind.String);
key.Close();
ActivePrinter = "VolianPDF Writer";
PrintOut(true);
do
{
// Console.WriteLine("Background Printing Status = {0}", _MyApp.BackgroundPrintingStatus);
Application.DoEvents();
} while (BackgroundPrintingStatus == 1);
ActivePrinter = printer;
Registry.CurrentUser.DeleteSubKeyTree(@"Software\VolianPDF Writer");
return pdfFileName;
}
catch (Exception ex)
{
throw new Exception("Error creating PDF - LBApplicationClass.CreatePDF2003BG", ex);
}
}
public string CreatePDF2007(string pdfFileName, bool openPDF)
{
pdfFileName = CreatePDF2007(pdfFileName);
if (openPDF) OpenPDF(pdfFileName);
return pdfFileName;
}
public string CreatePDF2007(string pdfFileName)
{
pdfFileName = AvailableFileName(pdfFileName);
if (!WordPDFExporterInstalled)
throw new Exception("MS Word PDF Exporter is not installed");
try
{
ActiveDocument.ExportAsFixedFormat(pdfFileName, LBWdExportFormat.wdExportFormatPDF);
return pdfFileName;
}
catch (Exception ex)
{
throw new Exception("Error creating PDF - LBApplicationClass.CreatePDF2007", ex);
}
}
}
public partial class LBDocuments
{
public LBDocumentClass Open(string fileName, Boolean addToRecentFiles)
{
return Open(fileName, Missing.Value, Missing.Value, addToRecentFiles);
}
private LBDocumentClass Open(params object[] myParams)
{
return new LBDocumentClass(InvokeMethod("Open", myParams));
}
}
public partial class LBDocumentClass : LBComObject
{
public void SaveAs2000(string fileName)
{
if (fileName.ToUpper().EndsWith("DOC"))
SaveAs2000(fileName, LBWdSaveFormat.wdFormatDocument);
else if (fileName.ToUpper().EndsWith("RTF"))
SaveAs2000(fileName, LBWdSaveFormat.wdFormatRTF);
else
SaveAs2000(fileName);
}
public void SaveAs2000(params object[] myParams)
{
InvokeMethod("SaveAs2000", myParams);
}
public void SaveAs(string fileName)
{
if (fileName.ToUpper().EndsWith("DOC"))
SaveAs2(fileName, LBWdSaveFormat.wdFormatDocument, Missing.Value, Missing.Value, false);
else if (fileName.ToUpper().EndsWith("RTF"))
SaveAs2(fileName, LBWdSaveFormat.wdFormatRTF, Missing.Value, Missing.Value, false);
else if (fileName.ToUpper().EndsWith("TXT"))
SaveAs2(fileName, LBWdSaveFormat.wdFormatDOSText, Missing.Value, Missing.Value, false);
else if (fileName.ToUpper().EndsWith("DOCX"))
SaveAs2(fileName, LBWdSaveFormat.wdFormatXMLDocument, Missing.Value, Missing.Value, false);
else
SaveAs2(fileName, Missing.Value, Missing.Value, Missing.Value, false);
}
public void SaveAs2(params object[] myParams)
{
InvokeMethod("SaveAs", myParams);
}
public void SaveAs(string fileName, LBWdSaveFormat format)
{
SaveAs2(fileName, format, Missing.Value, Missing.Value, false);
}
}
public partial class LBFind
{
public void ReplaceAll()
{
this.Execute(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, LBWdReplace.wdReplaceAll, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
}
}
public partial class LBFontClass : LBComObject
{
public int TextColor
{
get { return (int)GetProperty("Color"); }
set { SetProperty("Color", value); }
}
}
public partial class LBSelection : LBComObject
{
public Object get_Information(LBWdInformation info)
{
return GetProperty("Information", info);
}
public int MoveStart(LBWdUnits Unit, int Count)
{
return InvokeMethod("MoveStart", Unit, Count) as int? ?? 0;
}
public int MoveEnd(LBWdUnits Unit, int Count)
{
return InvokeMethod("MoveEnd", Unit, Count) as int? ?? 0;
}
public int EndKey(LBWdUnits Unit, bool Extend)
{
return InvokeMethod("EndKey", Unit, Extend) as int? ?? 0;
}
}
}