web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

Getting error while printing Document

(0) ShareShare
ReportReport
Posted on by 245

Hello Experts , in my code I wrote code to print a Document like below

Print()

{

IF NOT TryPrintDocument(Path,Name) THEN
ERROR('Not able to print %1. \\ERROR: %2',Path+Name, GETLASTERRORTEXT)

}

[TryFunction] TryPrintDocument(PathP : Text;NameP : Text)

{
ProcessStartInfo := ProcessStartInfo.ProcessStartInfo(PathP+NameP);
WITH ProcessStartInfo DO BEGIN
Verb := 'Print';
CreateNoWindow := TRUE;
WindowStyle := ProcessWindowStyle.Hidden;
END;
Process.Start(ProcessStartInfo);
CLEAR(ProcessStartInfo);
CLEAR(Process);
CLEAR(ProcessStartInfo);

}

But  While running this code I am getting error 

Error : 

Not able to print C:\Temp\test.pdf.

ERROR: A call to System.Diagnostics.Process.Start failed with this message: No application is associated with the specified file for this operation

I have the same question (0)
  • Lars Lohndorf-Larsen Profile Picture
    on at

    Hi, a bit of a guess - maybe just check that the system has a default printer?

    If not, then do you know what line it errors on, and what kind of variable is ProcessStartInfo?

  • Manikanta_501 Profile Picture
    245 on at

    Hello Lars Lohndorf-Larsen

    In my system default printer is setup.

    I am getting an error at Process.Start(ProcessStartInfo); Line

    and ProcessStartInfo is DotNet Variable .

  • Lars Lohndorf-Larsen Profile Picture
    on at

    Hi,

    Thanks for more details. What sub type is the DotNet variable? Maybe if you can attach or copy in the whole object (exported as txt)? And would we be able to reproduce it?

  • Manikanta_501 Profile Picture
    245 on at

    Subtype of ProcessStartInfo DotNet Variable is --> System.Diagnostics.ProcessStartInfo.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

  • Lars Lohndorf-Larsen Profile Picture
    on at

    Hi, and thanks for more details. I can also repro now. I am pretty sure that this is a setting in Windows as for example mentioned here https://stackoverflow.com/questions/21032151/no-application-is-associated-with-the-specified-file-for-this-operation-vb-net . You will see that the code works fine on a .docx file, but not on a .pdf file. And you see the same if you right-click these types of files in Windows Explorer - .docx has a "Print" option. .Pdf doesn't.

    I don't know if you need to install another pdf renderer - mine is set to MSEdge, or mabye somewhere in registry can fix it, but you need to get Windows to add a "print"-action to pdf documents somehow. Sorry I am not sure about how to do that but I hope this helps you forward and let us know if you find out,

    /Lars

    Repro code:

    //Print()

    Path := 'c:\temp\';
    Name := 'Doc.docx';
    Name := 'Test.pdf';

    ///IF FALSE THEN
    IF NOT TryPrintDocument(Path,Name) THEN
    ERROR('Not able to print %1. \\ERROR: %2',Path+Name, GETLASTERRORTEXT);

    ///TryPrintDocument(Path,Name);

    MESSAGE('Done.');
    END;

    }
    CODE
    {
    VAR
    ProcessStartInfo@1000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.ProcessStartInfo";
    Process@1001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process";
    Path@1002 : Text;
    Name@1003 : Text;

    [TryFunction]
    [LineStart(15)]
    LOCAL PROCEDURE TryPrintDocument@1(PathP@1000 : Text;NameP@1001 : Text);
    BEGIN
    ProcessStartInfo := ProcessStartInfo.ProcessStartInfo(PathP+NameP);
    WITH ProcessStartInfo DO BEGIN
    Verb := 'Print';
    ///Verb := '';
    CreateNoWindow := TRUE;
    ///WindowStyle := ProcessWindowStyle.Hidden;
    END;
    Process.Start(ProcessStartInfo);
    CLEAR(ProcessStartInfo);
    CLEAR(Process);
    CLEAR(ProcessStartInfo);
    END;

  • Lars Lohndorf-Larsen Profile Picture
    on at

    Just one more thing:

    If you need to research this further in non-NAV forums, then C/AL example won't help much, so below is C# code that can reproduce the same thing completely outside of NAV, in case it can help you bring this up in Windows / c# forums.

    hth

    Lars

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net.Configuration;

    using System.Net.Http;

    using System.Text;

    using System.Threading.Tasks;

    using System.Diagnostics;

    namespace ConsoleApp1

    {

       class Program

       {

           static void Main(string[] args)

           {

               System.Diagnostics.ProcessStartInfo ProcessStartInfo = new ProcessStartInfo("c:\\temp\\Test.pdf");

               ProcessStartInfo.Verb = "print";

               System.Diagnostics.Process Process = new Process();

               Process.Start(ProcessStartInfo);

               Console.WriteLine("Done");

               Console.ReadKey();

           }

       }

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,010

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,270 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,085 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans