Added PDF Generation Code

This commit is contained in:
Rich 2009-11-04 17:16:26 +00:00
parent f26cc50c3c
commit c5c4d6006b
2 changed files with 238 additions and 8 deletions

View File

@ -121,6 +121,19 @@ namespace LBWordLibrary
{
get { return new LBWindow(GetProperty("ActiveWindow")); }
}
public String Version
{
get { return (GetProperty("Version").ToString()); }
}
public String ActivePrinter
{
get { return (GetProperty("ActivePrinter").ToString()); }
set { SetProperty("ActivePrinter", value); }
}
public int BackgroundPrintingStatus
{
get { return (GetProperty("BackgroundPrintingStatus") as int? ?? 0); }
}
public void Quit()
{
InvokeMethod("Quit", Missing.Value, Missing.Value, Missing.Value);
@ -129,6 +142,14 @@ namespace LBWordLibrary
{
InvokeMethod("Quit", SaveChanges, OriginalFormat, RouteDocument);
}
public void PrintOut()
{
InvokeMethod("PrintOut", 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, Missing.Value);
}
public void PrintOut(object Background, object Append, object Range, object OutputFileName, object From, object To, object Item, object Copies, object Pages, object PageType, object PrintToFile, object Collate, object FileName, object ActivePrinterMacGX, object ManualDuplexPrint, object PrintZoomColumn, object PrintZoomRow, object PrintZoomPaperWidth, object PrintZoomPaperHeight)
{
InvokeMethod("PrintOut", Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight);
}
}
public partial class LBDocuments : LBComObjectList<LBDocuments, LBDocumentClass> /* Collection */
{
@ -249,6 +270,14 @@ namespace LBWordLibrary
{
InvokeMethod("Close", SaveChanges, OriginalFormat, RouteDocument);
}
public void ExportAsFixedFormat(string OutputFileName, LBWdExportFormat ExportFormat)
{
InvokeMethod("ExportAsFixedFormat", OutputFileName, ExportFormat, 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 void ExportAsFixedFormat(string OutputFileName, LBWdExportFormat ExportFormat, Boolean OpenAfterExport, LBWdExportOptimizeFor OptimizeFor, LBWdExportRange Range, int From, int To, LBWdExportItem Item, Boolean IncludeDocProps, Boolean KeepIRM, LBWdExportCreateBookmarks CreateBookmarks, Boolean DocStructureTags, Boolean BitmapMissingFonts, Boolean UseISO19005_1, object FixedFormatExtClassPtr)
{
InvokeMethod("ExportAsFixedFormat", OutputFileName, ExportFormat, OpenAfterExport, OptimizeFor, Range, From, To, Item, IncludeDocProps, KeepIRM, CreateBookmarks, DocStructureTags, BitmapMissingFonts, UseISO19005_1, FixedFormatExtClassPtr);
}
}
public partial class LBWindow : LBComObject
{
@ -276,6 +305,10 @@ namespace LBWordLibrary
get { return (LBWdColorIndex)GetProperty("HighlightColorIndex"); }
set { SetProperty("HighlightColorIndex", value); }
}
public LBParagraphFormatClass ParagraphFormat
{
get { return new LBParagraphFormatClass(GetProperty("ParagraphFormat")); }
}
}
public partial class LBParagraphFormatClass : LBComObject
{
@ -438,6 +471,34 @@ namespace LBWordLibrary
wdInWordMail = 37,
wdInClipboard = 38
}
public enum LBWdExportFormat
{
wdExportFormatPDF = 17,
wdExportFormatXPS = 18
}
public enum LBWdExportOptimizeFor
{
wdExportOptimizeForPrint = 0,
wdExportOptimizeForOnScreen = 1
}
public enum LBWdExportRange
{
wdExportAllDocument = 0,
wdExportSelection = 1,
wdExportCurrentPage = 2,
wdExportFromTo = 3
}
public enum LBWdExportItem
{
wdExportDocumentContent = 0,
wdExportDocumentWithMarkup = 7
}
public enum LBWdExportCreateBookmarks
{
wdExportCreateNoBookmarks = 0,
wdExportCreateHeadingBookmarks = 1,
wdExportCreateWordBookmarks = 2
}
public partial class LBPane : LBComObject
{
public LBPane() { }
@ -604,7 +665,7 @@ namespace LBWordLibrary
wdFormatDOSText = 4,
wdFormatDOSTextLineBreaks = 5,
wdFormatRTF = 6,
wdFormatEncodedText = 7,
wdFormatUnicodeText = 7,
wdFormatHTML = 8,
wdFormatWebArchive = 9,
wdFormatFilteredHTML = 10,
@ -619,7 +680,8 @@ namespace LBWordLibrary
wdFormatFlatXML = 19,
wdFormatFlatXMLMacroEnabled = 20,
wdFormatFlatXMLTemplate = 21,
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatFlatXMLTemplateMacroEnabled = 22,
wdFormatOpenDocumentText = 23
}
public enum LBWdFindMatch
{
@ -654,10 +716,6 @@ namespace LBWordLibrary
{
public LBPage() { }
public LBPage(Object item) : base(item) { }
public LBRectangles Rectangles
{
get { return new LBRectangles(GetProperty("Rectangles")); }
}
public int Height
{
get { return (GetProperty("Height") as int? ?? 0); }
@ -666,6 +724,10 @@ namespace LBWordLibrary
{
get { return (GetProperty("Left") as int? ?? 0); }
}
public LBRectangles Rectangles
{
get { return new LBRectangles(GetProperty("Rectangles")); }
}
public int Top
{
get { return (GetProperty("Top") as int? ?? 0); }

View File

@ -2,9 +2,177 @@ 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) System.Diagnostics.Process.Start(pdfFileName);
return pdfFileName;
}
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) System.Diagnostics.Process.Start(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) System.Diagnostics.Process.Start(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) System.Diagnostics.Process.Start(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)
@ -34,7 +202,7 @@ namespace LBWordLibrary
public void SaveAs(string fileName)
{
if (fileName.ToUpper().EndsWith("DOC"))
SaveAs2(fileName, LBWdSaveFormat.wdFormatDocument, Missing.Value, Missing.Value,false );
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"))
@ -58,7 +226,7 @@ namespace LBWordLibrary
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, Missing.Value, LBWdReplace.wdReplaceAll, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
}
}