How to Print PDFs Silently in .NET WPF Using SumatraPDF (No Admin Rights Needed)

 

Introduction


Do you need to print PDF files from your .NET WPF application without any pop-up dialogs or preview windows? In this guide, you’ll learn how to use the lightweight, free, and portable SumatraPDF tool to print PDFs silently—no admin rights or installation required!



Why Choose SumatraPDF for Silent PDF Printing in .NET?


SumatraPDF is the ideal solution for developers who want to print PDFs from their .NET apps. Here’s why:


  • Lightweight and Fast: Minimal footprint, quick startup.

  • Free and Open Source: No licensing issues.

  • No Installation Needed: Just copy the EXE—no admin rights required.

  • Command-Line Support: Easily automate printing from your code.

  • Perfect for .NET Integration: Bundle it directly in your project.



Step 1: Add SumatraPDF to Your .NET Project


  1. Download the portable version of SumatraPDF.exe from the official website.

  2. Create a Packages folder in your project’s root directory.

  3. Place SumatraPDF.exe inside the Packages folder.


Project structure:

MyApp/
 ├── Packages/
 │    └── SumatraPDF.exe
 └── MyApp.csproj


Step 2: Include SumatraPDF in Your Publish Output


To ensure SumatraPDF.exe is copied during build and publish, add this to your .csproj file:


<ItemGroup>
  <Content Include="Packages\**\*.*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>


Step 3: Use AppContext for Reliable File Paths


Always use AppContext.BaseDirectory to locate files at runtime:


string sumatraPath = Path.Combine(AppContext.BaseDirectory, "Packages", "SumatraPDF.exe");


Tip: Avoid using Directory.GetParent(...)—it may break after publishing.


Step 4: Print PDFs Silently from Your .NET Code


Here’s a sample code snippet to print a PDF file silently:


string printerName = "YourPrinterName";
string pdfFilePath = @"C:\Path\To\Your.pdf";

string arguments = $"-print-to \"{printerName}\" -print-settings \"1x\" \"{pdfFilePath}\" -silent -exit-when-done";

ProcessStartInfo psi = new()
{
    FileName = sumatraPath,
    Arguments = arguments,
    CreateNoWindow = true,
    UseShellExecute = false
};

Process.Start(psi);


Options explained:

  • -silent: No UI or pop-ups.

  • -exit-when-done: Closes SumatraPDF after printing.

  • -print-to: Specifies the printer.

  • -print-settings "1x": Prints one copy.



Step 5: Publish and Test Your Application


When you publish your app (using dotnet publish or Visual Studio):


  • Ensure the Packages folder is included in your output.

  • Confirm SumatraPDF.exe is accessible via AppContext.BaseDirectory.



Additional Tips for Printing PDFs in .NET


  • Orientation: SumatraPDF prints the PDF as-is—no need to adjust rotation or scaling.

  • Printer Drivers: Make sure the target machine has the correct printer drivers installed.

  • Advanced Options: Explore duplex, grayscale, and more in the SumatraPDF documentation.



Frequently Asked Questions (FAQ)


Q: Can I print PDFs silently in .NET without installing extra software?

A: Yes! With SumatraPDF’s portable version, you don’t need to install anything or require admin rights.


Q: Does SumatraPDF support silent printing on all printers?

A: It works with any printer installed on the system. Just make sure the printer drivers are installed.


Q: Can I customize print settings (copies, duplex, etc.)?

A: Yes, SumatraPDF supports various command-line options for advanced printing. See the official docs.


Q: Will this work after I publish my .NET app?

A: Yes, as long as you include the Packages folder and use AppContext.BaseDirectory to locate SumatraPDF.



Conclusion


Now you know how to print PDFs silently in your .NET WPF application using SumatraPDF—no admin rights, no preview dialogs, and no installation required. This method is perfect for POS systems, kiosks, or any app where you want printing to “just work.”


Ready to add silent PDF printing to your .NET app? Try it out and let me know how it goes in the comments!

Comments

Popular posts from this blog

Asynchronous Programming in .NET: Understanding async/await

Design Patterns In C# .NET (2023)