Welcome
Guest
•
Login
PDFsharp and MigraDoc Wiki
Navigation
¶
Main Page
Random Page
Create a new Page
All Pages
Categories
Navigation Paths
File Management
Create Account
Quick Search
»
Back
PDFsharp Sample: Protect Document
Modified on 2015/09/14 10:21
by
Thomas Hövel
Categorized as
PDFsharp Samples
,
Samples
{s:navigationPrevUpNext|PrivateFonts-sample|PDFsharpSamples|SplitDocument-sample} This sample shows how to protect a document with a password. ==PDF Output File== See the PDF file created by this sample: {s:PdfLink|ProtectDocument-sample%2fProtectDocument_output.pdf|output.pdf} (3 kB){br} {s:note} the owner password is "owner". ==Screen Shots== Here are the sample screen shots: {s:ImageThumbLink|Adobe Reader prompts for password|ProtectDocument-sample%2fProtectDocumentth.png|ProtectDocument-sample%2fProtectDocument.png} {s:ImageThumbLink|File is marked as SECURED|ProtectDocument-sample%2fProtectDocument2th.png|ProtectDocument-sample%2fProtectDocument2.png}{br} {s:note} Adobe Reader prompts for the password when you try to open the file (remember the secret password is "owner"); when the file is opened, the caption indicates "SECURED". ==Source Code== This is the whole source code needed to create the PDF file: {s:beginCsharp} // Get a fresh copy of the sample PDF file const string filenameSource = "HelloWorld.pdf"; const string filenameDest = "HelloWorld_tempfile.pdf"; File.Copy(Path.Combine("../../../../../PDFs/", filenameSource), Path.Combine(Directory.GetCurrentDirectory(), filenameDest), true); // Open an existing document. Providing an unrequired password is ignored. PdfDocument document = PdfReader.Open(filenameDest, "some text"); PdfSecuritySettings securitySettings = document.SecuritySettings; // Setting one of the passwords automatically sets the security level to // PdfDocumentSecurityLevel.Encrypted128Bit. securitySettings.UserPassword = "user"; securitySettings.OwnerPassword = "owner"; // Don't use 40 bit encryption unless needed for compatibility //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit; // Restrict some rights. securitySettings.PermitAccessibilityExtractContent = false; securitySettings.PermitAnnotations = false; securitySettings.PermitAssembleDocument = false; securitySettings.PermitExtractContent = false; securitySettings.PermitFormsFill = true; securitySettings.PermitFullQualityPrint = false; securitySettings.PermitModifyDocument = true; securitySettings.PermitPrint = false; // Save the document... document.Save(filenameDest); // ...and start a viewer. Process.Start(filenameDest); {s:endCsharp} {s:sampleSourceCode}
Meta Keywords:
Meta Description:
Change Comment:
Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles
ScrewTurn Wiki version 3.0.5.600. Some of the icons created by
FamFamFam
.
Impressum - Privacy Policy, Data Protection Declaration, Legal Notice