Reputation: 53
I'm investigating a batch printing solution.
The files to be printed will be in various (typical) formats, e.g., PDF, Word, Excel, etc. From what I've gathered (and tested) printing XPS files is the way to go on a Windows platform.
However, it's completely unclear to me how one actually creates an XPS file - without intimate knowledge of the input file format in question (programmatically that is).
My hope was that I could print to the local Microsoft XPS Document Writer "printer" and then physically print its output (i.e., the XPS file).
I'm unable to do this successfully programmatically. I've tried with managed code System.Printing
, unmanaged code Winspool API
.
I can successfully open the printer and write raw data to it, but I never get an XPS file output. How does one create XPS files? I've looked at the XPSDocumentWriter
API but this seems very complex and, presumably, has already been implemented by the Microsoft XPS Document Writer and/or existing applications.
Upvotes: 3
Views: 2061
Reputation: 430
I have found one way of doing it is starting with the application that created the document ie. Word, Excel etc and making them print. This piece of code takes a string to the document to be converted, makes a xps file in the users %tmp% folder and returns the string to the document. It does the job but it is not fast though:
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;
}
Upvotes: 3