I need to print a pdf document that I have altered without saving it back as a new pdf document. This code below works without a problem. However I would like to do this quite differently and I´m simultaneously having a brain-lag and can´t see the solution.
My code example
byte[] result;
using (MemoryStream ms = new MemoryStream())
{
PdfReader pdfReader = new PdfReader("c:\\templatePdf.pdf");
PdfStamper pdfStamper = new PdfStamper(pdfReader, ms);
/* abbreviated but here I alter the template pdf */
pdfStamper.FormFlattening = true;
pdfStamper.Close();
result = ms.GetBuffer();
}
/* Instead of saving a new file I would rather like to print
the altered template pdf in memory and then discard it */
using (FileStream fs = File.Create("C:\\Test.pdf"))
{
fs.Write(result, 0, (int)result.Length);
}
Process process = new Process();
process.StartInfo.FileName = "C:\\Test.pdf";
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = "\"" + ppr_PrinterDropDown.Text + "\"";
process.Start();
File.Delete("C:\\Test.pdf");
If you are using a file-based API, then you will struggle to do it without a file. You might be able to setup a named pipe server, but frankly that is a huge fiddle. I would, however, be tempted to look around for a fully managed PDF library with print support. But ultimately… what harm is the file system doing, really? Probably not a lot. I might suggest a few tweaks, though:
Path.GetTempPath()), notC:\Test