tags:

views:

8510

answers:

5

From an application I'm building I need to print existing PDFs (created by another app). How can I do this in C# and provide a mechanism so the user can select a different printer or other properties.

I've looked at the PrintDialog but not sure what file it is attempting to print, if any, b/c the output is always a blank page. Maybe I'm just missing something there.

Do I need to use "iTextSharp" (as suggested else where)? That seems odd to me since I can "send the the file to the printer" I just don't have any nice dialog before hand to set the printer etc. and I don't really want to write a printing dialog from the ground up but it seems like a lot of examples I found by searching did just that.

Any advice, examples or sample code would be great!

Also if PDF is the issue the files could be created by the other app in a diff format such as bitmap or png if that makes things easier.

Thanks, Cody

A: 

You will need Acrobat or some other application that can print the PDF. From there you P/Invoke to ShellExecute to print the document.

plinth
Can you pass printer selection and other parameters with that call?
Aaron Fischer
+6  A: 

Display a little dialog with a combobox that has its Items set to the string collection returned by PrinterSettings.InstalledPrinters.

If you can make it a requirement that GSView be installed on the machine, you can then silently print the PDF. It's a little slow and roundabout but at least you don't have to pop up Acrobat.

Here's some code I use to print out some PDFs that I get back from a UPS Web service:

    private void PrintFormPdfData(byte[] formPdfData)
    {
        string tempFile;

        tempFile = Path.GetTempFileName();

        using (FileStream fs = new FileStream(tempFile, FileMode.Create))
        {
            fs.Write(formPdfData, 0, formPdfData.Length);
            fs.Flush();
        }

        try
        {
            string gsArguments;
            string gsLocation;
            ProcessStartInfo gsProcessInfo;
            Process gsProcess;

            gsArguments = string.Format("-grey -noquery -printer \"HP LaserJet 5M\" \"{0}\"", tempFile);
            gsLocation = @"C:\Program Files\Ghostgum\gsview\gsprint.exe";

            gsProcessInfo = new ProcessStartInfo();
            gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
            gsProcessInfo.FileName = gsLocation;
            gsProcessInfo.Arguments = gsArguments;

            gsProcess = Process.Start(gsProcessInfo);
            gsProcess.WaitForExit();
        }
        finally
        {
            File.Delete(tempFile);
        }
    }

As you can see, it takes the PDF data as a byte array, writes it to a temp file, and launches gsprint.exe to print the file silently to the named printer ("HP Laserjet 5M"). You could replace the printer name with whatever the user chose in your dialog box.

Printing a PNG or GIF would be much easier -- just extend the PrintDocument class and use the normal print dialog provided by Windows Forms.

Good luck!

Nicholas Piasecki
+1  A: 

Although this is VB you can easily translate it. By the way Adobe does not pop up, it only prints the pdf and then goes away.

''' <summary>
''' Start Adobe Process to print document
''' </summary>
''' <param name="p"></param>
''' <remarks></remarks>
Private Function printDoc(ByVal p As PrintObj) As PrintObj
    Dim myProcess As New Process()
    Dim myProcessStartInfo As New ProcessStartInfo(adobePath)
    Dim errMsg As String = String.Empty
    Dim outFile As String = String.Empty
    myProcessStartInfo.UseShellExecute = False
    myProcessStartInfo.RedirectStandardOutput = True
    myProcessStartInfo.RedirectStandardError = True

    Try

        If canIprintFile(p.sourceFolder & p.sourceFileName) Then
            isAdobeRunning(p)'Make sure Adobe is not running; wait till it's done
            Try
                myProcessStartInfo.Arguments = " /t " & """" & p.sourceFolder & p.sourceFileName & """" & " " & """" & p.destination & """"
                myProcess.StartInfo = myProcessStartInfo
                myProcess.Start()
                myProcess.CloseMainWindow()
                isAdobeRunning(p)
                myProcess.Dispose()
            Catch ex As Exception
            End Try
            p.result = "OK"
        Else
            p.result = "The file that the Document Printer is tryng to print is missing."
            sendMailNotification("The file that the Document Printer is tryng to print" & vbCrLf & _
            "is missing. The file in question is: " & vbCrLf & _
            p.sourceFolder & p.sourceFileName, p)
        End If
    Catch ex As Exception
        p.result = ex.Message
        sendMailNotification(ex.Message, p)
    Finally
        myProcess.Dispose()
    End Try
    Return p
End Function
+1  A: 

You could also use PDFsharp - it's an open source library for creating and manipulating PDFs. http://www.pdfsharp.net/

Duffman
A: 

Here is article, I hope this will help

http://waqarahmadbhatti.blogspot.com/

Waqar Ahmad Bhatti