【发布时间】:2018-10-10 13:14:03
【问题描述】:
我有一个小类,可以在版本 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,但某些字体存在错误)
【问题讨论】:
-
什么是 PdfCreator?
-
您应该将其包含在您的问题中。不要假设人们知道它是什么。如果标签存在,则对其进行标记或包含链接。
-
已经这样做了
-
什么是精确问题?
标签: c# excel pdf com pdf-generation