EvotecIT / PSWritePDF

PowerShell Module to create, edit, split, merge PDF files on Windows / Linux and MacOS
GNU Affero General Public License v3.0
195 stars 21 forks source link

PDF Form Field Flattening #12

Closed lkeyes closed 3 years ago

lkeyes commented 4 years ago

Hi...I'm attempting to craft a Powershell solution which merges a series of existing PDF Forms, which "flattens" the forms prior to adding them to the merged PDF. I want the original data displayed that was put into the form, but I don't care that the form functionality is lost. I was hoping this might be available in PSWritePDF. -- Thanks!

PrzemyslawKlys commented 4 years ago

What happens if you try to merge them?

lkeyes commented 4 years ago

Hi..if I try to merge them before flattening, the field data are overwritten on each page. So, the result is that there will be multiple form pages, with each field containing the same data.

In Adobe Pro, the "Optimize" function will fix this....it strips out the form layer and just leaves the data visible. Once optimized, the form can't be used as a form anymore.... it simply displays whatever was entered originally on the form.

On Mon, Aug 10, 2020 at 3:18 PM Przemysław Kłys notifications@github.com wrote:

What happens if you try to merge them?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/EvotecIT/PSWritePDF/issues/12#issuecomment-671539194, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6A3JOGKQI7KV6KXPHZ5UTSABBXLANCNFSM4P2FUQDA .

-- Best wishes,

Larry Keyes lkeyes70@gmail.com

markdem commented 3 years ago

Can I please revive this request? I also need to be able to flatten the PDF. I am too much of a sissy to fix this issue myself and not too sure how to use github anyway, but the fix would be simply;

  1. add a ps optional parameter to Set-PDFform, somthing like -flatten
  2. if the pram is true, just run this line "$PDFAcroForm.flattenFields();"

Have a look at https://kb.itextpdf.com/home/it7kb/examples/flattening-a-form.

Thanks, and great work.

markdem commented 3 years ago

I have just tested this, and it works fine. I cant work out how to change it here. Any chance someone else can do it (or tell me how)?

2 things need to be changed;

  1. add "[bool] $Flatten" to the param list of "Set-PDFForm"
  2. add " if($Flatten) {$PDFAcroForm.flattenFields()}" just before "$PDF.Close()" at the end.

I have tested with the param set to true, false and null. all works as expected

PrzemyslawKlys commented 3 years ago

The "online" way to do it is simply edit the file in GitHub.

image

When you do that, a new fork is created and you get your own version of code. Then you can keep editing until you're happy. And then you get an option to create PR.

My recommendations:

  1. Make it a switch
  2. Make it possible to support setting up the form and then at the end if it user chooses Set-PDFForm -FlattenForm it would make it flat

But also make it possible that if no FIeldNameAndValueHashtable is provided - it still works as expected just flattening the form.

It would be interesting to "improve" Merge-PDF with [switch] $FlattenForms or similar so that if there is a form detected it would flatten it during merge if requested.

markdem commented 3 years ago

Cool, learned something new today. Thanks! Good point with the switch too. Not sure why I did not think of that. Suggestion 2 already works that way. If no hash table is provided, the foreach will do nothing, still check the $Flatten switch and flatten empty. Tested this too.

If I get some spare time in the next few days, I will have a look at Merge-PDF. Should be doable.

PrzemyslawKlys commented 3 years ago

This is now live. Thank you @markdem - I've improved the cmdlet a bit and added a test for flattening. I really appreciate it.