Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mako and Universal Print
var printDocument = job.Documents.First();
var jobFile = new FileInfo("temp.oxps");
await using (var documentStream = await client.Print.Printers[printerId].Jobs[jobId].Documents[printDocument.Id].Content.Request().GetAsync())
{
Console.WriteLine("Downloading...");
await using var file = File.Create(jobFile.FullName);
await documentStream.CopyToAsync(file);
}
Formats.Convert.OpenXpsToXps(jobFile, new FileInfo("temp-input.xps"));
Console.WriteLine("Redacting...");
using var xpsInput = IXPSInput.create(mako);
using var assembly = xpsInput.open("temp-input.xps");
using var document = assembly.getDocument();
using var page = document.getPage();
// Modify our page using Mako
using var xpsOutput = IXPSOutput.create(mako);
xpsOutput.writeAssembly(assembly, "temp-output.xps");
Console.WriteLine("Converting...");
Formats.Convert.XpsToOpenXps(new FileInfo("temp-output.xps"), jobFile);
long uploadSize;
await using (var modifiedDocumentStream = File.OpenRead(jobFile.FullName))
uploadSize = modifiedDocumentStream.Length;
var printDocumentUploadProperties = new PrintDocumentUploadProperties
{
ContentType = "application/oxps",
DocumentName = $"[Redacted] {printDocument.DisplayName}",
Size = uploadSize
};
var uploadSession = await client.Print.Printers[printerId].Jobs[jobId]
.Documents[printDocument.Id].CreateUploadSession(printDocumentUploadProperties).Request()
.PostAsync();
using var webClient = new WebClient();
webClient.Headers.Add("Content-Range", $"bytes 0-{uploadSize - 1}/{uploadSize}");
await webClient.UploadFileTaskAsync(uploadSession.UploadUrl, "PUT", jobFile.FullName);
await client.Print.Printers[printerId].Jobs[jobId].Redirect(redirectPrinterId, GetJobConfig()).Request().PostAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment