Forum Discussion
basilic001
May 17, 2024Copper Contributor
Regenerate a PDF file with a PDF printer
Hello.
We have issues with PDF files which need to be re-generated, using the "microsoft print to PDF" printer fixed issues that we have.
How can I code that in C#? I have tried heaps of codes, nothing worked.
I have tried this :
try
{
ProcessStartInfo info = new ProcessStartInfo
{
FileName = filePath,
Verb = "PrintTo",
Arguments = "\"" + outputFilePath + "\" \"Microsoft Print to PDF\"",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
using (Process p = new Process())
{
p.StartInfo = info;
p.Start();
p.WaitForExit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
but it's not adapter for Windows, or even :
try
{
PrintDocument pd = new PrintDocument();
ProcessStartInfo info = new ProcessStartInfo(filePath);
info.Verb = "Print";
pd.PrinterSettings.PrintToFile = true;
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
pd.PrinterSettings.PrintFileName = outputFilePath;
pd.Print();
info.CreateNoWindow = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
which generates empty files...
Thanks
1 Reply
- Abhishek_KhatriBrass ContributorTo programmatically print a PDF file to a new PDF file using the "Microsoft Print to PDF" printer in C#, you can use the PrintDocument class and the PrintControllerWithStatusDialog class to manage the printing process. The issue with generating empty files might be related to how the PrintDocument class handles the printing.
Here’s a more detailed example that should help you achieve your goal:
Add a reference to System.Drawing and System.Drawing.Printing in your project.
Use the following code to print the PDF:
using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
namespace PdfPrinter
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetDefaultPrinter(string name);
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\input.pdf";
string outputFilePath = @"C:\path\to\your\output.pdf";
try
{
PrintToPdf(filePath, outputFilePath);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
static void PrintToPdf(string inputFilePath, string outputFilePath)
{
var printerName = "Microsoft Print to PDF";
// Set the default printer to Microsoft Print to PDF
if (!SetDefaultPrinter(printerName))
{
throw new Exception("Could not set the default printer.");
}
// Create the print process
ProcessStartInfo printProcessInfo = new ProcessStartInfo
{
Verb = "print",
FileName = inputFilePath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
// Create a temp print document
using (PrintDocument printDocument = new PrintDocument())
{
printDocument.PrinterSettings.PrinterName = printerName;
printDocument.PrinterSettings.PrintToFile = true;
printDocument.PrinterSettings.PrintFileName = outputFilePath;
printDocument.PrintController = new StandardPrintController(); // Suppress the Print Dialog
// Start the print process
using (Process printProcess = Process.Start(printProcessInfo))
{
if (printProcess != null)
{
printProcess.WaitForExit();
}
}
}
}
}
}