Programmatically set filename and path in Microsoft Print to PDF printer

后端 未结 3 1261
春和景丽
春和景丽 2021-01-18 02:30

I have a C# .net program that creates various documents. These documents should be stored in different locations and with different, clearly defined names.

3条回答
  •  旧巷少年郎
    2021-01-18 03:22

    I did some experimenting myself but like yourself was also not able to prefill the SaveAs dialog in the PrintDialog with the DocumentName or PrinterSettings.PrintFileName already filled in the PrintDocument instance. This seems counterintuitive, so maybe I'm missing something

    If you want to, you can however bypass the printdialog and print automatically without any user interaction at all. If you choose to do so, you must make sure beforehand that the directory to which you want to add a document to is in existence and that the document to be added is not in existence.

    string existingPathName = @"C:\Users\UserName\Documents";
    string notExistingFileName = @"C:\Users\UserName\Documents\TestPrinting1.pdf";
    
    if (Directory.Exists(existingPathName) && !File.Exists(notExistingFileName))
    {
        PrintDocument pdoc = new PrintDocument();
        pdoc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
        pdoc.PrinterSettings.PrintFileName = notExistingFileName;
        pdoc.PrinterSettings.PrintToFile = true;
        pdoc.PrintPage += pdoc_PrintPage;
        pdoc.Print();
     }
    

提交回复
热议问题