I need to convert XPS to PDF. The conversion needs to be executed within a .NET 6.0 (C#) application code. I've tried various methods and currently settled on using Ghostscript. Here's a snippet of the code:
var pdfStream = new MemoryStream();
var ghostscriptPath = "/usr/bin/gs";
using (var process = new Process())
{
var xpsFilePath = "/mnt/d/Users/Lev/adcm/18/ADCM_Connect/PilotWebApplication/Microsoft+Word+-+ВОР+АД+изм.1 (2).xps"; // Specify the path to your XPS file
var outputPdfPath = "/mnt/d/Users/Lev/adcm/18/ADCM_Connect/PilotWebApplication/output.pdf"; // Specify the path to save the PDF file
// Set command line parameters for Ghostscript
process.StartInfo.FileName = ghostscriptPath;
process.StartInfo.Arguments = $"-q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=\"{outputPdfPath}\" \"{xpsFilePath}\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
// Start the process
process.Start();
// Wait for the process to exit
process.WaitForExit();
}
pdfStream.Position = 0;
var encodedFileName = System.Web.HttpUtility.UrlEncode(fileData.name);
Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{System.IO.Path.ChangeExtension(encodedFileName, ".pdf")}\"");
I'm encountering an error "GPL Ghostscript 9.55.0: Unrecoverable error, exit code 1". Is it possible to solve this task using Ghostscript, and what are the methods to convert XPS to PDF using .NET 6.0?