Création de fichier XPS avec Microsoft XPS Document Writer par programmation

J’enquête sur une solution d’impression par lot.

Les fichiers à imprimer seront dans différents formats (par exemple, PDF, Word, Excel, etc.). À partir de ce que j’ai rassemblé (et testé), l’impression de fichiers XPS est la voie à suivre sur une plate-forme Windows.

Cependant, je ne comprends pas du tout comment créer un fichier XPS – sans avoir une connaissance approfondie du format du fichier en entrée (par programmation, c’est le cas).

J’espérais pouvoir imprimer sur l’imprimante «Imprimante» Microsoft XPS locale, puis imprimer physiquement sa sortie (c.-à-d. Le fichier XPS).

Je suis incapable de le faire avec succès par programmation. J’ai essayé avec le code managé System.Printing , code non Winspool API .

Je parviens à ouvrir l’imprimante et à y écrire des données brutes, mais je ne reçois jamais de sortie de fichier XPS. Comment créer des fichiers XPS? J’ai examiné l’API XPSDocumentWriter mais cela semble très complexe et, probablement, a déjà été implémenté par Microsoft XPS Document Writer et / ou des applications existantes.

J’ai trouvé une façon de le faire est de commencer par l’application qui a créé le document, c.-à-d. Word, Excel etc et les faire imprimer. Ce morceau de code prend une chaîne dans le document à convertir, crée un fichier xps dans le dossier% tmp% utilisateurs et renvoie la chaîne au document. Il fait le travail mais ce n’est pas rapide si:

  private readonly ssortingng TEMP = Environment.ExpandEnvironmentVariables("%tmp%"); private object nullObject = Type.Missing; private ssortingng ConvertWordtoXps(ssortingng wordDocName) { Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Documents.Open(wordDocName, ConfirmConversions: false, ReadOnly: true); ssortingng xpsFileName = Ssortingng.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 ssortingng ConvertExceltoXps(ssortingng excelWorkbookName) { Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); excelApp.Workbooks.Add(excelWorkbookName); Workbook excelWorkbook = excelApp.ActiveWorkbook; ssortingng xpsFileName = Ssortingng.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 ssortingng ConvertPowerPointtoXps(ssortingng 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); ssortingng xpsFileName = Ssortingng.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; }