We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Open- set UserPassword- Close -> invalid document

The code is very straightforward, the resulting file cannot be opened in Acrobat Reader, the document is not a valid pdf (there was an error processing the page).


static void Main(string[] args)
{
Stream stream = new FileStream(@“C:\Documents and Settings\Administrator\My Documents\Bosch Crailsheim 20091202.pdf”, FileMode.Open);
Pdf pdf = new Pdf(stream);
pdf.Security = new Security {UserPassword = “1234”};
stream =
new FileStream(
@“C:\Documents and Settings\Administrator\My Documents\Bosch Crailsheim 20091202 secure.pdf”,
FileMode.OpenOrCreate);
//pdf.Save(stream);
pdf.Close();
stream.Close();
}

Hi Luca,


Thanks for your inquiry. Please note Aspose.Pdf.Generator namespace is used to create a file from scratch. You can use Aspose.Pdf.Generator.Security class only to set security setting of newly created file, so you are getting a corrupt file. To encrypt an existing file you can use Aspose.Pdf.Document or Aspose.Pdf.Facades namespaces. Please check following documentation links for the purpose. Hopefully it will help you to accomplish the task.


Please feel free to contact us for any further assistance.

Best Regards,

luca.maselli:
The code is very straightforward, the resulting file cannot be opened in Acrobat Reader, the document is not a valid pdf (there was an error processing the page).

static void Main(string[] args)
{
Stream stream = new FileStream(@“C:\Documents and Settings\Administrator\My Documents\Bosch Crailsheim 20091202.pdf”, FileMode.Open);
Pdf pdf = new Pdf(stream);
pdf.Security = new Security {UserPassword = “1234”};
stream =
new FileStream(
@“C:\Documents and Settings\Administrator\My Documents\Bosch Crailsheim 20091202 secure.pdf”,
FileMode.OpenOrCreate);
//pdf.Save(stream);
pdf.Close();
stream.Close();
}

Hi Luca,

Thanks for using our API’s.

Adding more to Tilal’s comments, the above stated approach which you have specified is Direct to File creation mode of Aspose.Pdf.Generator namespace and if you still want to create new PDF document using this approach and apply security restrictions (encrypt the file), please try using the following updated code. For your reference, I have also attached the resultant PDF generated over my end.

[C#]

Stream stream = new FileStream(@“C:\pdftest\Bosch
Crailsheim 20091202.pdf”
, FileMode.CreateNew);<o:p></o:p>

Pdf pdf = new Pdf(stream);

pdf.Security = new Security { UserPassword = "1234" };

//stream = new FileStream( @"C:\Documents and Settings\Administrator\My Documents\Bosch Crailsheim 20091202 secure.pdf",FileMode.OpenOrCreate);

//pdf.Save(stream);

pdf.Close();

stream.Close();