Gajawada
Gajawada

Reputation: 69

Print a PDF Document Programmatically

How can i Print a Pdf document through programmatically?

i am using the follwing code to print a PDF file.but when i click on print icon directly it starts printing.but i dont want it.

 <asp:ImageButton ID="PrintButton" runat="server" ImageUrl="~/images/print-icon.png"
                   OnClick="PrintButton_Click" ToolTip="Print Document" />

My Cs Code is

protected void PrintButton_Click(object sender, EventArgs e)
    {
        ProcessStartInfo infoPrint = new ProcessStartInfo();
        infoPrint.FileName = Session["filename"].ToString();
        infoPrint.Verb = "PrintTo";
        infoPrint.CreateNoWindow = true;
        infoPrint.WindowStyle = ProcessWindowStyle.Normal;
        infoPrint.UseShellExecute = true;
        Process printProcess = new Process();
        printProcess = Process.Start(infoPrint);            

    }      

i want to open a print dialog box when the user clicks on print icon.if the user clicks on Print Button in Print dialog box then i want to start printing the document. My PDF file is in a folder on the server i want it to be printed through programmatically in asp.net.

Upvotes: 2

Views: 2849

Answers (2)

r2b2s
r2b2s

Reputation: 213

Have a look at This posst

this code add javascript line to print the pdf

Public Shared Function PrintJStoPDF(thePDF As Byte(), direct As Boolean) As Byte()


    Dim BB As Byte() = Nothing

    Using ms As New MemoryStream
        Using reader As New PdfReader(thePDF)
            Dim stamper = New PdfStamper(reader, ms)

            Dim jsText As String = "var res = app.setTimeOut('this.print({bUI: true, bSilent: " & direct.ToString.ToLower & ", bShrinkToFit: false});', 200);"

            stamper.JavaScript = jsText

            stamper.FormFlattening = True
            stamper.Writer.CloseStream = False
            stamper.Close()


            ms.Position = 0

            BB = ms.ToArray
        End Using
    End Using

    Return BB

End Function

Upvotes: 0

Jason Meckley
Jason Meckley

Reputation: 7591

this code will run on the server not the client. while developing the server and client are the same machine, your local workstation. Once deployed, this would execute on the server, not the user's local work station.

you can open a print dialog box using javascript

window.print();

However that will print the entire webapge, not the document specifically.

If you would like to print only the PDF, you need to stream the file to browser (not an entire webform). The user can then take advantage of the native print options within the adobe reader. There are many examples online about how to stream documents to the client's browser.

Upvotes: 2

Related Questions