Print PDF to another PDF with powershell to flatten form fields

Kevin Albrecht 1 Reputation point
2021-01-08T22:01:16.143+00:00

Hey,

We have customer provided PDF files that allot of times contain layers, which require us flattening them. For awhile, I have been trying to get AdobePDF printer to script from within powershell, but it requires registry hacks, and I think it is unreliable, although it seems to work OK for others.

All of the examples of using pdfsharp to flatten forms are written in C# & do not seem to translate to PS very easily (for me). I have search around & around, but have not found a solution. I have resolved myself tyo try & find a solution of printing to a PDF printer. It seems that there would be hooks into MS Print to PDF, but I cant find any.

Does anyone have some advice?

  • Thanks, Kevin
Windows for business | Windows Server | User experience | PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Lance Haugen 1 Reputation point
    2021-03-04T15:56:01.453+00:00

    I am also trying to do this. I am struggling with adding the page content and get an empty PDF file as a result. Here is my script so far - maybe together we can figure this out!

    function ConvertTo-PDF {
    param(
    $OriginalFilePath
    )
    Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.IO

    $PrinterSettings = new-Object System.Drawing.Printing.PrinterSettings
    $PrinterSettings.PrinterName = 'Microsoft Print to PDF'
    $PrinterSettings.PrintToFile = $true
    
    $PrintDocument = New-Object System.Drawing.Printing.PrintDocument
    $PrintDocument.DocumentName = $OriginalFilePath
    $PrintDocument.PrinterSettings = $PrinterSettings
    
    $PictureByteArray = [System.IO.File]::ReadAllBytes($OriginalFilePath)
    
    #add the page content to the file - this isn't working
    #$streamToPrint = New-Object System.IO.StreamReader $OriginalFilePath
    #$PrintDocument.add_PrintPage($PrintPageHandler)
    #$PrintDocument.add_PrintPage($OriginalFilePath)
    
    #another attempt to add the page content - also not working
    #$PrintDocument.add_PrintPage({
        # Create an ImageConverter to convert our byte array into a Bitmap for drawing
    #    $ImageConverter = New-Object System.Drawing.ImageConverter
    #    [Drawing.Bitmap]$ProfileImg = $Imageconverter.ConvertFrom($PictureByteArray)
    
        # Set an arbitrary width to scale the image to
    #    $Width = $ProfileImg.Width
    
        # Calculate the relationship between height and width to calculate with new width
    #    $ProfileScale = [double]$ProfileImg.Height / [double]$ProfileImg.Width
    
        # Create source rectangle representing full size of image
    #    $ProfileRectSrc = New-Object Drawing.RectangleF(0, 0, $ProfileImg.Width, $ProfileImg.Height)
        # Change scale of destination rectangle based on the calculated width
    #    $ProfileRectDest = New-Object Drawing.RectangleF([System.Drawing.PointF]::new(10,10), [System.Drawing.Size]::new($Width, [int]($Width * $ProfileScale)))
    
    #    $_.Graphics.DrawImage($ProfileImg, $ProfileRectDest, $ProfileRectSrc, [Drawing.GraphicsUnit]::Pixel) 
    #})
    
    
    $file=[io.fileinfo]$OriginalFilePath
    $OutputFile = [io.path]::Combine($file.DirectoryName, $file.BaseName) + '_1.pdf'
    $PrintDocument.PrinterSettings.PrintFileName = $OutputFile
    
    
    $PrintDocument.Print()
    $PrintDocument.Dispose()
    

    }

    ConvertTo-PDF 'C:\Original.pdf'


  2. Rich Matheisen 48,026 Reputation points
    2023-07-28T15:08:09.33+00:00

    Have you tried the PSPrintPDF module (https://www.powershellgallery.com/packages/PSWritePDF/0.0.19)?

    It does use 3rd-party software (iText 7, available here: https://github.com/itext/itext7-dotnet, but also part of the Powershell module per the AGPL license).

    According to the modules' change log:

    0.0.17 - 2021.05.23 ☑ Set-PDFForm can now flatten forms with Flatten switch. Thank you markdem! in #23


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.