Search code examples
c#model-view-controllerprintingadobe-readervirtual-path

Print pdf file from a virtual path


I am using this code to print myDocument.pdf file from drive D: which is working.

    Process proc = new Process();
    proc.StartInfo.Verb = "PrinTo";
    proc.StartInfo.FileName = @"C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe";
    proc.StartInfo.Arguments = @"/p /h D:myDocument.pdf";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.Start();

    proc.WaitForInputIdle();
    System.Threading.Thread.Sleep(1000);
    if (false == proc.CloseMainWindow())
    proc.Kill();

But i want to print a file from the folder inside my project which is Content/report/myDocument.pdf. i have tried to change 'proc.StartInfo.Arguments = @"/p /h D:myDocument.pdf";' to:

proc.StartInfo.Arguments = Server.MapPath("~/Content/report/myDocument.pdf");
proc.StartInfo.Arguments = @"Content/report/myDocument.pdf";
proc.StartInfo.Arguments "C:\Users\User\Documents\Visual Studio 2012\Projects\PDF\PDF\Content\report\myDocument.pdf";

All of that are not working and adobe reader says that the file cannot be found.

Note: I removed "/p /h" which is the command to print and minimize adobe reader just to try if adobe reader will find the myDocument.pdf file.

What is wrong in my paths? Thanks in advance.


Solution

  • Have you tried enclosing the file name in double-quotes?

    proc.StartInfo.Arguments = @"""C:\Users\User\Documents\Visual Studio 2012\Projects\PDF\PDF\Content\report\Voucher.pdf""";
    

    As the filename contains spaces it may be that Acrobat Reader was trying to load a file called C:\Users\User\Documents\Visual, which I assume does not exist.

    If you want to reintroduce the /p and /h switches, try

    proc.StartInfo.Arguments = @"/p /h ""C:\Users\User\Documents\Visual Studio 2012\Projects\PDF\PDF\Content\report\Voucher.pdf""";
    

    If you want to use a file relative to the virtual path of a web application, then try

    string filePath = Server.MapPath("~/Content/report/Voucher.pdf");
    proc.StartInfo.Arguments = string.Format(@"/p /h ""{0}""", filePath);
    

    Note however that this will print from the machine that you are running the web application on. If you are running it out of Visual Studio on your computer, then it will print from your computer. If however you have published your web app to IIS on a server somewhere and are viewing your web app from a different computer, the PDF will attempt to print from the server, not from your computer.