How to print existing pdf code on code page?

I saw several questions like SO, but couldn't find anything suitable for me. The chain of events that I would like to happen is as follows:

  • User clicks ASP.NET control button
  • This calls this onclick event button, which is the foo () function in C # codebehind
  • foo () calls some other (non-essential) function that creates a PDF file that ends up being saved on the server. This function returns the path to the PDF.
  • Without any other user interaction, once the PDF is generated, a print dialog opens in the user's browser to print this PDF

What do I need to do to complete step 4? Ideally, this would be something that I can call in foo () by passing the path to the PDF file that will invoke the print dialog in the user browser (print the PDF file, not the page that onclick launches from).

I think that I could forward the URL of the PDF document and paste some Javascript into the PDF, which automatically prints it, but I would prefer - I do not necessarily want to print the PDF every time it was opened (in the browser). Any other good way to do this?

+3
source share
5 answers

The solution I came to was the following:

- ASP.NET( BinaryData.aspx), PDF. Page_Load, :

protected void Page_Load(object sender, System.EventArgs e)
{
    //Set the appropriate ContentType.
    Response.ContentType = "Application/pdf";
    Response.AppendHeader("Pragma", "no-cache"); 
    Response.AppendHeader("Cache-Control", "no-cache");
    //Get the physical path to the file.
    string FilePath = (string)Session["fileLocation"];
    if ( FilePath != null )         
    {
        string FileName = Path.GetFileName(FilePath);

        Response.AppendHeader("Content-Disposition", "attachment; filename="+FileName);

        //Write the file directly to the HTTP content output stream.
        Response.WriteFile(FilePath);
        Response.End();
    }
}

PDF Session "fileLocation". , , , , Response.Redirect("BinaryData.aspx").

, PDF ( ).

0
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=myfilename.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(ms.ToArray())

ms = , ( .)

, , :

Response.WriteFile("c:\pathtofile.pdf")
+1

Vdex. iText/#.

PdfAction action = new PdfAction();
action.Put(new PdfName("Type"), new PdfName("Action"));
action.Put(new PdfName("S"), new PdfName("Named"));
action.Put(new PdfName("N"), new PdfName("Print"));

PdfReader reader = new PdfReader(ReportFile.FullFilePath(reportFile.FilePath));

PdfStamper stamper = new PdfStamper(reader, Response.OutputStream);
stamper.Writer.CloseStream = false;
stamper.Writer.SetOpenAction(action);
stamper.Close();
+1

, :

javascript, PDF, javascript javascript.

, , , yourpdf.pdf? print, , , , PDF .

0

:

, PDF, PDF . , abcpdf ASP, :

Set oDoc = Server.CreateObject("ABCpdf4.Doc")
oDoc.SetInfo oDoc.Root, "/OpenAction", "<< /Type /Action /S /Named /N /Print >>"

Obviously, the code will look different depending on which PDF authoring tool you use ...

0
source

Source: https://habr.com/ru/post/1714245/


All Articles