gpt4 book ai didi

windows - 使用 Microsoft XPS Document Writer 以编程方式创建 XPS 文件

转载 作者:可可西里 更新时间:2023-11-01 09:25:30 26 4
gpt4 key购买 nike

我正在研究批量打印解决方案。

要打印的文件将采用各种(典型)格式,例如 PDF、Word、Excel 等。根据我收集(和测试)的结果,打印 XPS 文件是在 Windows 平台上进行的方式。

但是,我完全不清楚如何实际创建 XPS 文件 - 不熟悉所讨论的输入文件格式(以编程方式)。

我希望我可以打印到本地 Microsoft XPS Document Writer“打印机”,然后实际打印它的输出(即 XPS 文件)。

我无法以编程方式成功执行此操作。我尝试过托管代码 System.Printing,非托管代码 Winspool API

我可以成功打开打印机并向其写入原始数据,但我从未得到 XPS 文件输出。如何创建 XPS 文件?我查看了 XPSDocumentWriter API,但这看起来非常复杂,并且大概已经由 Microsoft XPS Document Writer 和/或现有应用程序实现。

最佳答案

我发现一种方法是从创建文档的应用程序开始,即。 Word、Excel 等并打印出来。这段代码将一个字符串带到要转换的文档中,在用户的%tmp% 文件夹中创建一个xps 文件并将该字符串返回到文档中。它完成了工作,但速度并不快:

    private readonly string TEMP = Environment.ExpandEnvironmentVariables("%tmp%");
private object nullObject = Type.Missing;

private string ConvertWordtoXps(string wordDocName)
{
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Documents.Open(wordDocName, ConfirmConversions: false, ReadOnly: true);
string xpsFileName = String.Concat(TEMP, "\\", Path.GetFileNameWithoutExtension(wordDocName), ".xps");

try
{
wordApp.ActiveDocument.SaveAs2(xpsFileName, FileFormat: WdSaveFormat.wdFormatXPS);
return xpsFileName;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
((Microsoft.Office.Interop.Word._Application)wordApp).Quit(SaveChanges: false, OriginalFormat: nullObject, RouteDocument: nullObject);
}
return null;

}

private string ConvertExceltoXps(string excelWorkbookName)
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Workbooks.Add(excelWorkbookName);
Workbook excelWorkbook = excelApp.ActiveWorkbook;
string xpsFileName = String.Concat(TEMP, "\\", Path.GetFileNameWithoutExtension(excelWorkbookName), ".xps");

try
{
excelWorkbook.ExportAsFixedFormat(
XlFixedFormatType.xlTypeXPS,
xpsFileName,
Quality: XlFixedFormatQuality.xlQualityMinimum,
IncludeDocProperties: false,
IgnorePrintAreas: false,
From: nullObject,
To: nullObject,
OpenAfterPublish: false,
FixedFormatExtClassPtr: nullObject
);

return xpsFileName;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
excelWorkbook.Close(XlSaveAction.xlDoNotSaveChanges, Filename: nullObject, RouteWorkbook: nullObject);
((Microsoft.Office.Interop.Excel._Application)excelApp).Quit();
}
return null;
}

private string ConvertPowerPointtoXps(string pptFile)
{
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations pptSet = pptApp.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptSet.Open(pptFile, ReadOnly: MsoTriState.msoTrue, Untitled: MsoTriState.msoTrue, WithWindow: MsoTriState.msoFalse);
string xpsFileName = String.Concat(TEMP, "\\", Path.GetFileNameWithoutExtension(pptFile), ".xps");

try
{
pptPresentation.ExportAsFixedFormat(
xpsFileName,
PpFixedFormatType.ppFixedFormatTypeXPS,
PpFixedFormatIntent.ppFixedFormatIntentScreen,
FrameSlides: MsoTriState.msoFalse,
HandoutOrder: PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
OutputType: PpPrintOutputType.ppPrintOutputFourSlideHandouts,
PrintHiddenSlides: MsoTriState.msoFalse,
RangeType: PpPrintRangeType.ppPrintAll,
SlideShowName: "",
IncludeDocProperties: false,
KeepIRMSettings: true,
DocStructureTags: true,
BitmapMissingFonts: true,
UseISO19005_1: false,
ExternalExporter: nullObject
);
return xpsFileName;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
((_Presentation)pptPresentation).Close();
((Microsoft.Office.Interop.PowerPoint._Application)pptApp).Quit();
}
return null;
}

关于windows - 使用 Microsoft XPS Document Writer 以编程方式创建 XPS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13058565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com