简体   繁体   English

从MemoryStream打印pdf文档

[英]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. 我需要打印已经更改过的pdf文档,而不将其另存为新的pdf文档。 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. 如果您使用的是基于文件的API,那么您将很难在没有文件的情况下进行操作。 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. 但是,我很想四处寻找具有打印支持的完全托管的PDF库。 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 使用临时区域( Path.GetTempPath() ),而不是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. 首先,我们需要写入内存流,然后借助内存流方法“ WriteTo”,我们可以写入页面的响应,如以下代码所示。

   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. FormName是给定的文件名,此代码将通过调用PopUp使生成的PDF文件可下载。

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. 当然,这是假设您的打印机具有本机PDF支持。 Otherwise you will have to do the rendering work yourself on the client and send it not as a raw stream. 否则,您将必须自己在客户端上执行渲染工作,而不是将其作为原始流发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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