Adding password to a PDF document programmatically in .NET

I am using following code to add password to office document and pdf files

using (FileStream fs = new FileStream(FilePath, FileMode.Open))
{

                AddPasswordOptions options = new AddPasswordOptions(getFileFormat(FilePath), Password);
                GroupDocs.Merger.Domain.Result.DocumentResult result = new DocumentHandler().AddPassword(fs, options);

                fs.Close();
                Stream documentStream = result.Stream;
                var filestream = File.Create(OutputPath);

                documentStream.CopyTo(filestream);
                documentStream.Close();
                filestream.Close();

            }

My password it 255 charactors long for example like this

(BE$0iTrL9^OGe{{Mbc"aYaBvxFRt1lUys~Z(O\s<t_u’Wx_?D0{eR7e-28O[H^8}d%Sw"7SfnH+>Gr?#c-)Z4;Qs^/G=dO}?.bI.D=UT2i?iT.3Z4fnf;R@PP|;3,BM@A><)lx8ZfBnW$<"@pi3ZCVr6bXm.&3|kkLG2Gz}52.(]H@YX~Toh=qoCLy3%m[=*/PA}F(F}Xj’1H[dPIAWUrQy(j:MO^0qE0c%A=J%+"NM-QXhFc<2/+6haj/

after adding password pdf file cannot open manually by entering above password.
but word files works correctly

do yo have any idea of this

1 Like

@tilankak

This issue is reproduced at our end. Hence, we’ve logged it in our internal issue tracking system with ID MERGERNET-942. We’ll now further investigate it and let you know as there’s any update.

1 Like

@tilankak

We have an update on MERGERNET-942, there are some limits for the PDF formats related to it’s format specification. Usually, the PDF password could be from 4 to 32 symbols. GroupDocs.Merger API uses AES-256 encryption algorithm. Please read about password string length limit here.