使用 PdfCreator .NET COM 包装器打印 Excel sheet
Printing Excel sheet using PdfCreator .NET COM wrapper
我有一个小 class 可以与版本 2 之前的旧 PDF Creator 一起使用。它使用 PDF Creator
打印机将 Excel 工作表打印到指定的 PDF 文件,从而创建指定路径的 PDF 文件。
public class PDFCreatorHelper
{
//Global Deceleration
private clsPDFCreator _pdfcreator = null;
public string CreatedFile { get; private set; }
public bool ActiveXError { get { return _ActiveXError; } }
private bool _ActiveXError = false;
private void _pdfcreator_eError()
{
MessageBox.Show("PDF Creator: " + _pdfcreator.cError.Description, "PDF Creator", MessageBoxButton.OK, MessageBoxImage.Error);
}
public void Initialize()
{
try
{
if (_pdfcreator == null)
{
_pdfcreator = new clsPDFCreator();
_pdfcreator.eReady += new __clsPDFCreator_eReadyEventHandler(_pdfcreator_eReady);
_pdfcreator.eError += new __clsPDFCreator_eErrorEventHandler(_pdfcreator_eError);
}
}
catch (Exception ex)
{
_ActiveXError = true;
Tools.LogException(ex, "PDFCreator");
}
}
private void _pdfcreator_eReady()
{
//Returns path for generated PDF File
CreatedFile = _pdfcreator.cOutputFilename;
}
public void PrintSheet(Worksheet xlSheet, Microsoft.Office.Interop.Excel.Application app, string file)
{
try
{
Initialize();
if (_ActiveXError) return;
string parameters = "/NoProcessingAtStartup";
_pdfcreator = new clsPDFCreator();
if (!_pdfcreator.cStart(parameters, false))
MessageBox.Show("Nepodařilo se spustit \"PDFCreator\".", "Tisk sestavy", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
// Set parameters for saving the generating pdf automatically to a directory.
clsPDFCreatorOptions opt = _pdfcreator.cOptions;
opt.UseAutosave = 1;// Use auto save functionality.
opt.UseAutosaveDirectory = 1;// Use directory for saving the file.
string folder = Path.GetDirectoryName(file);
string filename = Path.GetFileName(file);
opt.AutosaveDirectory = folder; // Name of the output directory.
opt.AutosaveFormat = 0;// Format of file is to be saved. 0 if for pdf.
opt.AutosaveFilename = filename;// Name of the output file name.
opt.Papersize = "A4";
_pdfcreator.cOptions = opt;
_pdfcreator.cClearCache();
_pdfcreator.cDefaultPrinter = "PDFCreator";
string defaultPrinter = _pdfcreator.cDefaultPrinter;
xlSheet.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, "PDFCreator", Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Wait till doc gets queued up.
while (_pdfcreator.cCountOfPrintjobs != 1) ;
// Start the printer.
_pdfcreator.cPrinterStop = false;
// Wait till all doc get converted to pdf.
while (_pdfcreator.cCountOfPrintjobs != 0) ;
// Stop the printer.
_pdfcreator.cPrinterStop = true;
}
}
catch (Exception ex)
{
_ActiveXError = true;
Tools.LogException(ex, "PDFCreator");
}
finally
{
// Close the printer
if (_pdfcreator is clsPDFCreator) _pdfcreator.cClose();
_pdfcreator = null;
}
}
}
但我需要达到同样的效果with the new .NET COM wrapper。
几乎没有任何文档可用,对象模型也大不相同。我不知道如何启动 PDF Creator,在其上设置选项并在 excel 中打印到 PDFCreator 打印机。
pdfforge.PDFCreator.UI.ComWrapper.PdfCreatorObj o =
new pdfforge.PDFCreator.UI.ComWrapper.PdfCreatorObj();
pdfforge.PDFCreator.UI.ComWrapper.Printers p = o.GetPDFCreatorPrinters;
string printerName = o.GetPDFCreatorPrinters.GetPrinterByIndex(0);
...?
(我知道我可以从 Excel 保存为 PDF,但某些字体存在错误)
我在安装文件夹中找到了示例:
C:\Program Files\PDFCreator\COM Scripts\C#.Net\COM_TestForm
此外,整个项目都在 GitHub 上,源代码:
https://github.com/pdfforge/PDFCreator
我已经相应地修改了我的方法。问题是我的项目以 .NET 4.5 和 COMWrapper 4.5.1 为目标,因此构建失败,找不到 pdfcreator 命名空间。我已经从 Git 下载了 PDF Creator 并为 4.5 构建了 COMWrapper,它现在可以工作了。
只有一些错误调用 jobQueue.ReleaseCom()
,解决了重用一个队列的问题。
public static class PDFCreatorHelper
{
public static string ErrorText { get; private set; }
public static string CreatedFile { get; private set; }
private static pdfforge.PDFCreator.UI.ComWrapper.Queue jobQueue = null;
public static void PrintSheet(Worksheet xlSheet, Microsoft.Office.Interop.Excel.Application app, string file)
{
ErrorText = null;
CreatedFile = null;
if (jobQueue == null)
{
Type queueType = Type.GetTypeFromProgID("PDFCreator.JobQueue");
Activator.CreateInstance(queueType);
jobQueue = new pdfforge.PDFCreator.UI.ComWrapper.Queue();
jobQueue.Initialize(); //Reusing one instance for the application runtime
}
else
{
jobQueue.Clear(); //Delete jobs already put there
}
string folder = Path.GetDirectoryName(file);
string filename = Path.GetFileName(file);
string convertedFilePath = Path.Combine(folder, filename);
try
{
//Actual print command
xlSheet.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, "PDFCreator", Type.Missing, Type.Missing, Type.Missing, Type.Missing);
if (!jobQueue.WaitForJob(10))
{
ErrorText = string.Format("PDFCreator: tisk {0} nebyl spuštěn do 10 sekund.", file);
Tools.LogError(ErrorText);
}
else
{
var printJob = jobQueue.NextJob;
printJob.SetProfileByGuid("DefaultGuid");
printJob.SetProfileSetting("OpenViewer","false");
printJob.SetProfileSetting("OpenWithPdfArchitect", "false");
printJob.SetProfileSetting("ShowProgress", "false");
printJob.SetProfileSetting("TargetDirectory", folder);
printJob.SetProfileSetting("ShowAllNotifications", "false");
if (File.Exists(convertedFilePath)) File.Delete(convertedFilePath);
printJob.ConvertTo(convertedFilePath);
if (!printJob.IsFinished || !printJob.IsSuccessful)
{
ErrorText = string.Format("PDFCreator: nepodařila se konverze souboru: {0}.", file);
Tools.LogError(ErrorText);
}
printJob = null;
}
}
catch (Exception err)
{
Tools.LogException(err, "PDFCreator");
ErrorText = err.Message;
}
finally
{
CreatedFile = convertedFilePath;
//If this is left uncommented, app hangs during the second run
//jobQueue.ReleaseCom();
//jobQueue = null;
}
}
}
我有一个小 class 可以与版本 2 之前的旧 PDF Creator 一起使用。它使用 PDF Creator
打印机将 Excel 工作表打印到指定的 PDF 文件,从而创建指定路径的 PDF 文件。
public class PDFCreatorHelper
{
//Global Deceleration
private clsPDFCreator _pdfcreator = null;
public string CreatedFile { get; private set; }
public bool ActiveXError { get { return _ActiveXError; } }
private bool _ActiveXError = false;
private void _pdfcreator_eError()
{
MessageBox.Show("PDF Creator: " + _pdfcreator.cError.Description, "PDF Creator", MessageBoxButton.OK, MessageBoxImage.Error);
}
public void Initialize()
{
try
{
if (_pdfcreator == null)
{
_pdfcreator = new clsPDFCreator();
_pdfcreator.eReady += new __clsPDFCreator_eReadyEventHandler(_pdfcreator_eReady);
_pdfcreator.eError += new __clsPDFCreator_eErrorEventHandler(_pdfcreator_eError);
}
}
catch (Exception ex)
{
_ActiveXError = true;
Tools.LogException(ex, "PDFCreator");
}
}
private void _pdfcreator_eReady()
{
//Returns path for generated PDF File
CreatedFile = _pdfcreator.cOutputFilename;
}
public void PrintSheet(Worksheet xlSheet, Microsoft.Office.Interop.Excel.Application app, string file)
{
try
{
Initialize();
if (_ActiveXError) return;
string parameters = "/NoProcessingAtStartup";
_pdfcreator = new clsPDFCreator();
if (!_pdfcreator.cStart(parameters, false))
MessageBox.Show("Nepodařilo se spustit \"PDFCreator\".", "Tisk sestavy", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
// Set parameters for saving the generating pdf automatically to a directory.
clsPDFCreatorOptions opt = _pdfcreator.cOptions;
opt.UseAutosave = 1;// Use auto save functionality.
opt.UseAutosaveDirectory = 1;// Use directory for saving the file.
string folder = Path.GetDirectoryName(file);
string filename = Path.GetFileName(file);
opt.AutosaveDirectory = folder; // Name of the output directory.
opt.AutosaveFormat = 0;// Format of file is to be saved. 0 if for pdf.
opt.AutosaveFilename = filename;// Name of the output file name.
opt.Papersize = "A4";
_pdfcreator.cOptions = opt;
_pdfcreator.cClearCache();
_pdfcreator.cDefaultPrinter = "PDFCreator";
string defaultPrinter = _pdfcreator.cDefaultPrinter;
xlSheet.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, "PDFCreator", Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Wait till doc gets queued up.
while (_pdfcreator.cCountOfPrintjobs != 1) ;
// Start the printer.
_pdfcreator.cPrinterStop = false;
// Wait till all doc get converted to pdf.
while (_pdfcreator.cCountOfPrintjobs != 0) ;
// Stop the printer.
_pdfcreator.cPrinterStop = true;
}
}
catch (Exception ex)
{
_ActiveXError = true;
Tools.LogException(ex, "PDFCreator");
}
finally
{
// Close the printer
if (_pdfcreator is clsPDFCreator) _pdfcreator.cClose();
_pdfcreator = null;
}
}
}
但我需要达到同样的效果with the new .NET COM wrapper。 几乎没有任何文档可用,对象模型也大不相同。我不知道如何启动 PDF Creator,在其上设置选项并在 excel 中打印到 PDFCreator 打印机。
pdfforge.PDFCreator.UI.ComWrapper.PdfCreatorObj o =
new pdfforge.PDFCreator.UI.ComWrapper.PdfCreatorObj();
pdfforge.PDFCreator.UI.ComWrapper.Printers p = o.GetPDFCreatorPrinters;
string printerName = o.GetPDFCreatorPrinters.GetPrinterByIndex(0);
...?
(我知道我可以从 Excel 保存为 PDF,但某些字体存在错误)
我在安装文件夹中找到了示例:
C:\Program Files\PDFCreator\COM Scripts\C#.Net\COM_TestForm
此外,整个项目都在 GitHub 上,源代码:
https://github.com/pdfforge/PDFCreator
我已经相应地修改了我的方法。问题是我的项目以 .NET 4.5 和 COMWrapper 4.5.1 为目标,因此构建失败,找不到 pdfcreator 命名空间。我已经从 Git 下载了 PDF Creator 并为 4.5 构建了 COMWrapper,它现在可以工作了。
只有一些错误调用 jobQueue.ReleaseCom()
,解决了重用一个队列的问题。
public static class PDFCreatorHelper
{
public static string ErrorText { get; private set; }
public static string CreatedFile { get; private set; }
private static pdfforge.PDFCreator.UI.ComWrapper.Queue jobQueue = null;
public static void PrintSheet(Worksheet xlSheet, Microsoft.Office.Interop.Excel.Application app, string file)
{
ErrorText = null;
CreatedFile = null;
if (jobQueue == null)
{
Type queueType = Type.GetTypeFromProgID("PDFCreator.JobQueue");
Activator.CreateInstance(queueType);
jobQueue = new pdfforge.PDFCreator.UI.ComWrapper.Queue();
jobQueue.Initialize(); //Reusing one instance for the application runtime
}
else
{
jobQueue.Clear(); //Delete jobs already put there
}
string folder = Path.GetDirectoryName(file);
string filename = Path.GetFileName(file);
string convertedFilePath = Path.Combine(folder, filename);
try
{
//Actual print command
xlSheet.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, "PDFCreator", Type.Missing, Type.Missing, Type.Missing, Type.Missing);
if (!jobQueue.WaitForJob(10))
{
ErrorText = string.Format("PDFCreator: tisk {0} nebyl spuštěn do 10 sekund.", file);
Tools.LogError(ErrorText);
}
else
{
var printJob = jobQueue.NextJob;
printJob.SetProfileByGuid("DefaultGuid");
printJob.SetProfileSetting("OpenViewer","false");
printJob.SetProfileSetting("OpenWithPdfArchitect", "false");
printJob.SetProfileSetting("ShowProgress", "false");
printJob.SetProfileSetting("TargetDirectory", folder);
printJob.SetProfileSetting("ShowAllNotifications", "false");
if (File.Exists(convertedFilePath)) File.Delete(convertedFilePath);
printJob.ConvertTo(convertedFilePath);
if (!printJob.IsFinished || !printJob.IsSuccessful)
{
ErrorText = string.Format("PDFCreator: nepodařila se konverze souboru: {0}.", file);
Tools.LogError(ErrorText);
}
printJob = null;
}
}
catch (Exception err)
{
Tools.LogException(err, "PDFCreator");
ErrorText = err.Message;
}
finally
{
CreatedFile = convertedFilePath;
//If this is left uncommented, app hangs during the second run
//jobQueue.ReleaseCom();
//jobQueue = null;
}
}
}