简体   繁体   中英

Print pdf document from MemoryStream

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:

  1. use the temp area ( Path.GetTempPath() ), not C:\\Test
  2. wait for the process to finish before deleting the file

First We Need To Write into our Memory Stream and then with the help of Memory Stream method "WriteTo" we can write to the Response of the Page as shown in the below code.

   MemoryStream filecontent = null;
   filecontent =//CommonUtility.ExportToPdf(inputXMLtoXSLT);(This will be your MemeoryStream Content)
   Response.ContentType = "image/pdf";
   string headerValue = string.Format("attachment; filename={0}", formName.ToUpper() + ".pdf");
   Response.AppendHeader("Content-Disposition", headerValue);

   filecontent.WriteTo(Response.OutputStream);

   Response.End();

FormName is the fileName given,This code will make the generated PDF file downloadable by invoking a PopUp.

This is easy. The hard part is getting useful info about print status completion and total page numbers.

var pq = LocalPrintServer.GetDefaultPrintQueue();
var theJob = pq.AddJob();
try{
    using(var js = theJob.JobStream){
        var buffer = File.ReadAllBytes("yourPathToPdf");
        js.Write(buffer,0,buffer.Length);
    }
    var done=false;
    while(!done)
    {
        pq.Refresh();
        theJob.Refresh();
        done = theJob.IsCompleted || theJob.IsDeleted || theJob.IsPrinted;
    }
}
catch(Exception ex){
    //handle this
}
finally{
    theJob?.Dispose();
    pq?.Dispose();
}

This is assuming your printer has native PDF support, of course. Otherwise you will have to do the rendering work yourself on the client and send it not as a raw stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM