cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Programmatically print file to PDF

MC_9665161
4-Participant

Programmatically print file to PDF

We're thinking of using MathCAD Prime. One of our key requirements is to be able to programmatically print MathCAD Prime files to PDF (i.e. with no user interaction). I know this is possible with MathCAD 15 (you automate printing to PDF from the commandline with MathCAD 15 by adding the "-p" option after the executable and before the filename), but I cannot see how to do it with MathCAD Prime. I've been looking through the API documentation: https://support.ptc.com/help/mathcad/r7.0/en/index.html#page/PTC_Mathcad_Help%2Fmathcad_and_automation_api.html%23. It says that you can print MathCAD Prime worksheets, but I cannot actually find this documented anywhere.

Please could you let me know how to do this, either from the API or from the commandline?

Thanks for your help!

ACCEPTED SOLUTION

Accepted Solutions

I fear about related functionality up to Mathcad 15 (command mathcad.exe -p test.xmcd😞 this is not part of Mathcad Prime functionality

  • CS361245 How to call Mathcad from command to print a specific worksheet

Workaround: API functionality to save as PDF is introduced with Mathcad Prime 6.0

  • CS315481 for more information about how to print a worksheet as PDF through API
  • Details:
    • Use the API method:   IMathcadPrimeWorksheet3::SaveAs(string path)
    • Sample code:

Ptc.MathcadPrime.Automation.ApplicationCreator mathcad = new Ptc.MathcadPrime.Automation.ApplicationCreator();
           IMathcadPrimeWorksheet3 mathcadPrimeWorksheet = mathcad.Open("d:\\test.mcdx") as IMathcadPrimeWorksheet3;
         
           if (mathcadPrimeWorksheet != null)
           {
               mathcadPrimeWorksheet.SaveAs("d:\\test.pdf");
              //Just change the extension of the file here to print to other formats such as RTF.                
               mathcadPrimeWorksheet.Close(SaveOption.spDiscardChanges);
           }

 

  • Note:
    • Saving with PDF format is supported since Mathcad Prime 6.0.
    • Users can use Object Browser from Visual studio to see the list of all Mathcad Prime APIs, by loading the file Ptc.MathcadPrime.Automation.tlb from Mathcad Prime installation directory.
  • Related: CS338281 [Knowledge hub] Customization with Mathcad Prime

View solution in original post

18 REPLIES 18
ppal
17-Peridot
(To:MC_9665161)

I dont know the but perhaps this:

https://community.ptc.com/t5/PTC-Mathcad/Batch-Printing/td-p/209960 may have some answers.

 

https://www.fcoder.com/how-to/printing-video-tutorials

Print Conductor allows you to seamlessly print an unlimited number of CAD drawings including MathCAD, MathCAD Prime and Solid Edge drawings, multi-page DWFX drawings, and more. No CAD software required!

MC_9665161
4-Participant
(To:ppal)

Hi,

 

Thanks for your response.

  • The thread that you mention is for MathCAD 14 & 15, not MathCAD Prime, so it's not very relevant.
    • To automate publishing MathCAD 15 I ended up using the -p option and an old version of PDFCreator to print to a specific location with a specific naming convention. Once the PDF has been generated I then force close MathCAD 15 to get around the problem with the error message.
    • The other suggestion in that thread is AutoIt, which I have tried. It requires the window to be in focus to work and it is very fiddly to get all the codes for the click areas. It is flakey and does not always work. I ended up spending a lot of time building a solution that uses it, only to be disappointed and have to throw it away. I would not recommend it to anyone.
  •  Print Conductor does sound cool, but it seems like it still requires user intervention. If it can be automated then I would love to hear more about it.

 

What I'm really asking, is there a way to automate printing MathCAD Prime? Is there a -p option or is there a scripting language, e.g. VBscript, which you can use to print MathCAD Prime?

I fear about related functionality up to Mathcad 15 (command mathcad.exe -p test.xmcd😞 this is not part of Mathcad Prime functionality

  • CS361245 How to call Mathcad from command to print a specific worksheet

Workaround: API functionality to save as PDF is introduced with Mathcad Prime 6.0

  • CS315481 for more information about how to print a worksheet as PDF through API
  • Details:
    • Use the API method:   IMathcadPrimeWorksheet3::SaveAs(string path)
    • Sample code:

Ptc.MathcadPrime.Automation.ApplicationCreator mathcad = new Ptc.MathcadPrime.Automation.ApplicationCreator();
           IMathcadPrimeWorksheet3 mathcadPrimeWorksheet = mathcad.Open("d:\\test.mcdx") as IMathcadPrimeWorksheet3;
         
           if (mathcadPrimeWorksheet != null)
           {
               mathcadPrimeWorksheet.SaveAs("d:\\test.pdf");
              //Just change the extension of the file here to print to other formats such as RTF.                
               mathcadPrimeWorksheet.Close(SaveOption.spDiscardChanges);
           }

 

  • Note:
    • Saving with PDF format is supported since Mathcad Prime 6.0.
    • Users can use Object Browser from Visual studio to see the list of all Mathcad Prime APIs, by loading the file Ptc.MathcadPrime.Automation.tlb from Mathcad Prime installation directory.
  • Related: CS338281 [Knowledge hub] Customization with Mathcad Prime
MC_9665161
4-Participant
(To:MichaelW)

Hi, this is great, thanks! I'm guessing the code example you provided is .NET?

MC_9665161
4-Participant
(To:MichaelW)

Thanks for this. Out of curiosity, will it work for MathCAD Express as well?

Hi, sorry for the delay, 

 

1.

as I know the code will run compiled as C# (C Sharp) language code.

And yes, this is also supported by Microsoft's .NET Framework (for example, create a new Console App .NET Framework project).

 

2.

I am not sure about Mathcad Prime Express and API support - I could not find any such statement.

 

I guess a valid reason for that is PTC normally states what is supported, rather than what is not supported.

Another valid reason could be: Mathcad Prime Express is not a different product - it is rather a limited run mode of the full Mathcad Prime product in case a valid license is (even temporary) not available.

And third, since the Express mode is a restriction to specific known features, called Premium features, I cannot imagine that it restricts any customized feature as well (which is nothing but another coding library, but built from customized code by the customer).

 

I hope this helps,

Michael

DJNewman
17-Peridot
(To:MichaelW)

The API should work with Mathcad Express.

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.
MC_9665161
4-Participant
(To:DJNewman)

Fantastic, thanks!

MC_9665161
4-Participant
(To:MichaelW)

Thanks! I will try it out!

I have a full sub doing this, but in VBA. I don't know if this sub allows providing full code.


@MC_9665161 wrote:

Thanks for this. Out of curiosity, will it work for MathCAD Express as well?


Hi,

please read https://community.ptc.com/t5/Mathcad/Mathcad-Express-free-watermark/td-p/658145. I think you do not want to see watermark in PDF.


Martin Hanák
Mr_TDablju
4-Participant
(To:MC_9665161)

Hi

 

I found this topic very interesting so used my (little) programing skills to write simple GUI program for my own use, that allows batch printing of MCDX files into PDF and RTF files. Works quite fine, it processes as many files as you load into it (as long as you do not mess with it on purpose) 😀

 

Anyway, can share it from my side, but I am not sure what about legal thing of this. 

It is single exe file, that was buit on my computer and as I understand it contains MathCAD API *.dll burnt into it (which is not mine, but PTC owns it). So is here anyone from @admin PTC MathCAD that can say something about legal issue of sharing such exe for the comunity?

 

It hasn't been written for commercial purposes, just to speed up work and to learn C#, so I do not guarantee that it will work once I share it.

 

2023-08-10 09_12_18-Desktop.png

Mr_TDablju
4-Participant
(To:Mr_TDablju)

Couldn't edit my previous post, so adding reply.

 

I figured out how to not 'bake' Mathcad *.dll into the app, so sharing the app with you. Now app uses link to the installation directory and checks it every time you want to print.

PS: read the readme file first. If it will not work. Probably you installed the MathCAD not using default directory, so you must provide proper path in 'config' file.

Good luck!

h-man
3-Newcomer
(To:Mr_TDablju)

Thanks for the tool, but unfortunately the tool crashes (see error message below) despite the .dll is present and the .config file is correct. 

Does anybody have the same problem or - even better - knows a solution?

 

Informationen über das Aufrufen von JIT-Debuggen
anstelle dieses Dialogfelds finden Sie am Ende dieser Meldung.

************** Ausnahmetext **************
System.Runtime.InteropServices.COMException (0x80004004): Vorgang abgebrochen (Ausnahme von HRESULT: 0x80004004 (E_ABORT))
at Ptc.MathcadPrime.Automation.IMathcadPrimeWorksheet3.SaveAs(String fileName)
at Batch_MathCAD_Printer.MainForm.printFile(String sourceFileName, String sourceFilePath)
at Batch_MathCAD_Printer.MainForm.ExecuteBatch()
at Batch_MathCAD_Printer.MainForm.btnPrint_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)


************** Geladene Assemblys **************
System.Private.CoreLib
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Private.CoreLib.dll.
----------------------------------------
Batch MathCAD Printer
Assembly-Version: 1.0.0.0.
Win32-Version: n/v.
CodeBase: .
----------------------------------------
System.Runtime
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Runtime.dll.
----------------------------------------
System.Windows.Forms
Assembly-Version: 6.0.2.0.
Win32-Version: 6.0.1222.56810.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/System.Windows.Forms.dll.
----------------------------------------
System.ComponentModel.Primitives
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.ComponentModel.Primitives.dll.
----------------------------------------
System.Windows.Forms.Primitives
Assembly-Version: 6.0.2.0.
Win32-Version: 6.0.1222.56810.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/System.Windows.Forms.Primitives.dll.
----------------------------------------
System.Runtime.InteropServices
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Runtime.InteropServices.dll.
----------------------------------------
System.Drawing.Primitives
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Drawing.Primitives.dll.
----------------------------------------
System.Collections.Specialized
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Collections.Specialized.dll.
----------------------------------------
System.Threading
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Threading.dll.
----------------------------------------
System.Diagnostics.TraceSource
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Diagnostics.TraceSource.dll.
----------------------------------------
System.Collections
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Collections.dll.
----------------------------------------
System.Drawing.Common
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/System.Drawing.Common.dll.
----------------------------------------
Microsoft.Win32.Primitives
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/Microsoft.Win32.Primitives.dll.
----------------------------------------
System.ComponentModel.EventBasedAsync
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.ComponentModel.EventBasedAsync.dll.
----------------------------------------
System.Threading.Thread
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Threading.Thread.dll.
----------------------------------------
Accessibility
Assembly-Version: 4.0.0.0.
Win32-Version: 6.0.1222.56810.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/Accessibility.dll.
----------------------------------------
System.ComponentModel.TypeConverter
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.ComponentModel.TypeConverter.dll.
----------------------------------------
System.Numerics.Vectors
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Numerics.Vectors.dll.
----------------------------------------
Microsoft.Win32.SystemEvents
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/Microsoft.Win32.SystemEvents.dll.
----------------------------------------
System.Collections.Concurrent
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Collections.Concurrent.dll.
----------------------------------------
System.ComponentModel
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.ComponentModel.dll.
----------------------------------------
System.Memory
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Memory.dll.
----------------------------------------
System.Resources.Extensions
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/System.Resources.Extensions.dll.
----------------------------------------
System.Drawing
Assembly-Version: 6.0.2.0.
Win32-Version: 6.0.1222.56810.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/System.Drawing.dll.
----------------------------------------
System.ObjectModel
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.ObjectModel.dll.
----------------------------------------
System.Linq
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Linq.dll.
----------------------------------------
System.Collections.NonGeneric
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Collections.NonGeneric.dll.
----------------------------------------
System.Runtime.CompilerServices.Unsafe
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Runtime.CompilerServices.Unsafe.dll.
----------------------------------------
System.Configuration.ConfigurationManager
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/System.Configuration.ConfigurationManager.dll.
----------------------------------------
System.Runtime.InteropServices.RuntimeInformation
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Runtime.InteropServices.RuntimeInformation.dll.
----------------------------------------
System.Private.Uri
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Private.Uri.dll.
----------------------------------------
System.Diagnostics.Process
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Diagnostics.Process.dll.
----------------------------------------
System.Xml.ReaderWriter
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Xml.ReaderWriter.dll.
----------------------------------------
System.Private.Xml
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Private.Xml.dll.
----------------------------------------
System.Net.WebClient
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Net.WebClient.dll.
----------------------------------------
System.Text.Encoding.Extensions
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Text.Encoding.Extensions.dll.
----------------------------------------
Ptc.MathcadPrime.Automation
Assembly-Version: 9.0.0.0.
Win32-Version: 9.0.0.0.
CodeBase: file:///C:/Program%20Files/PTC/Mathcad%20Prime%209.0.0.0/Ptc.MathcadPrime.Automation.dll.
----------------------------------------
mscorlib
Assembly-Version: 4.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/mscorlib.dll.
----------------------------------------
System.Runtime.Loader
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Runtime.Loader.dll.
----------------------------------------
System.Windows.Forms.resources
Assembly-Version: 6.0.2.0.
Win32-Version: 6.0.1222.56810.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.WindowsDesktop.App/6.0.12/de/System.Windows.Forms.resources.dll.
----------------------------------------
System.Diagnostics.StackTrace
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Diagnostics.StackTrace.dll.
----------------------------------------
System.Reflection.Metadata
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Reflection.Metadata.dll.
----------------------------------------
System.Collections.Immutable
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Collections.Immutable.dll.
----------------------------------------
System.Diagnostics.FileVersionInfo
Assembly-Version: 6.0.0.0.
Win32-Version: 6.0.1222.56807.
CodeBase: file:///C:/Program%20Files/dotnet/shared/Microsoft.NETCore.App/6.0.12/System.Diagnostics.FileVersionInfo.dll.
----------------------------------------

************** JIT-Debuggen **************

 

Mr_TDablju
4-Participant
(To:h-man)

sorry, I don't get notifications on posts. Will answer as typical IT: it worked for me

 

Now I launched it after almost a year and see that there is a problem. Get the same error. Probably due to either new version of MathCAD or new Windows. Will try to figure it out.

Mr_TDablju
4-Participant
(To:h-man)

Ok.

 

I found the problem. C# is quite enigmatic in matter of errors. So the problem was related to the missing output directory. App was written on Windows 10, and at that time the default directory was hardcoded, since for many years desktop directory had typical path. SInce Windows 11, the desktop is located on One Drive automatically, so app didn't see it and throw an error. Now in version 2 it is corrected. Works on my windows 11.

 

New version is available on google drive (scanned by google): Batch MathCAD Printer-v2.zip 

Tool works great but it will only load MCDX and not XMCD files. 

probably because there is a separate mathcad license for legacy version of mathcad that allows to convert the file to new extension. Sorry.

 

Announcements

Top Tags