0

We receive a lot of PDFs and some are protected and others are not. When a PDF file is protected we cannot even select words to comment. One known way around this is to print to pdf from the browser to create a new pdf file which is unprotected. This manual work is quite cumbersome for my users as they upload the pdfs to our ASP.NET Core MVC application. So I am trying to automate this process. I found this question and answers below:

How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer that comes with Windows 10

The first answer allows silent printing to pdf using a windows service(which our IIS server has windows print to pdf as an available printer). I have modified the code to run inside a test controller and it seems to draw an error. I am using a mac so I am unable to test this locally however when I push this to IIS the controller errors out. In IIS print spooling is disabled and I am guessing that is for security reasons? So could this be what is stopping my code or is there an issue with my code? If it is because print spooling is disabled is there anyway to use this functionality with print spooling disabled?

        public IActionResult RemovePDFPassword()
    {
        StreamReader streamToPrint;

        var RootPath = _env.ContentRootPath;
        var fileDirectory = Path.Combine(Directory.GetParent(RootPath).Parent.ToString(), "files");
        var filePathSource = Path.Combine(fileDirectory, "testDocToPrint.pdf");
        var filePathDestination = Path.Combine(fileDirectory, "test.pdf");

        streamToPrint = new StreamReader(filePathSource);

        System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument
        {
            PrinterSettings = new PrinterSettings
            {
                PrinterName = "Microsoft Print to PDF",
                PrintToFile = true,
                PrintFileName = filePathDestination
            }
        };

        printDoc.Print();
        streamToPrint.Close();

        Console.ReadKey(true);

        return Ok("Success");
    }
Irish Redneck
  • 983
  • 7
  • 32

0 Answers0