Microsoft print to pdf

Hello.

Is there a way to programatically create, without going through the “Print” dialog, a .pdf document by providing a name and location for the file and using the built-in “microsoft print to pdf”

I can do it on Mac.

Thanks.

Lennox

There is a C# example. You would have to fins a way to declare the .Net functions.

Thanks Louis but I am lacking the “know-how” for this conversion, maybe someone can assist.

Thanks again.

Lennox

Hello,

I got this from Stack Overflow… the link provided by Louis D above

To print a PrintDocument object using the Microsoft Print to PDF printer without prompting for a filename, here is the pure code way to do this:
// generate a file name as the current date/time in unix timestamp format
string file = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();

// the directory to store the output.
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

// initialize PrintDocument object
PrintDocument doc = new PrintDocument() {
PrinterSettings = new PrinterSettings() {
// set the printer to ‘Microsoft Print to PDF’
PrinterName = “Microsoft Print to PDF”,

    // tell the object this document will print to file
    PrintToFile = true,

    // set the filename to whatever you like (full path)
    PrintFileName = Path.Combine(directory, file + ".pdf"),
}

};

doc.Print();

Can someone please convert that to Xojo?

Thanks.

Lennox

Check out Einhugur PDF Class Einhugur Plugin Releases (2020) - #38 by Björn_Eiríksson
And MBS DynaPDF Monkeybread Xojo plugin - DynaPDF

This can be of interest too:

Thanks Michel.
Lennox

@Lennox_Jacob1,

You don’t need plugins to print to .pdf using ‘Microsoft Print to PDF’ in a headless manner. Write a Powershell script then and call it from within Xojo using shell.execute. Then when comfortable script it all from within Xojo using an interactive shell. Essentially, you will be:

assign(ing) the printer a port where you use the output file path as port name. Or in other words, run this script to create a new printer that always prints to a file of your choice

This solution is documented here: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/sending-powershell-results-to-pdf-part-2

The example is simple enough to follow and works well. I use something very similar to this regularly.

I find Powershell a great companion for Xojo Development.

Kind regards, Andrew

Thanks Andrew, I will try it out.
Thanks again.
Lennox