For HTML2PDF and XML2PDF solution check our PDFDoc Scout ActiveX library instead
Changing security permissions of existing PDF document
using Bytescout.PDF
In this example Bytescout.PDF library for
.NET is used from Visual C# to
1) create password-protected PDF document with permission
settings allowing to print, copy etc ("HelloWorldWithSecurity.pdf")
2) take this existing PDF document ("HelloWorldWithSecurity.pdf")
and change its security permissions by removing "high
quality printing" permission (so document is allowed
to be printed in low-resolution only) and remove password
required to open document (although document remains protected).
Modified document is saved as "HelloWorldWithSecurity_HiPrintRemoved.pdf"
Download example source code: bytescoutpdf_change_permissons_of_existing_pdf.zip
(10 KB)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Bytescout.PDF;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
#region Create PDF with security
// Create main PDF Doc Engine
PDFDocEngine engine = new PDFDocEngine("", "");
// Add new document
Document document = engine.AddDocument();
// Append new page to the document
Page page = document.AddPage(PageSizeType.A3, PageOrientationType.LandScape);
// Create new drawing
Drawing drawing = page.AddDrawing();
// Add standard font
uint font = document.AddFontStandard(StandardFontType.Courier,
FontEncodingType.WinAnsi);
// Set Active Font
drawing.SetActiveFont(font, 50, false, false);
// Draw Text
drawing.PlaceText(100, 100, 0, "Hello World!");
// Closing drawing on the page
drawing.Close();
document.AddProtection(
DocumentPermissionType.AssembleDocument |
DocumentPermissionType.CopyInformation |
DocumentPermissionType.ExtractTextAndImages |
DocumentPermissionType.FillAcroForm |
DocumentPermissionType.ModifyAnnotation |
DocumentPermissionType.ModifyContent |
DocumentPermissionType.Print | DocumentPermissionType.PrintHiResolution,
SecurityKeyLength._128Bit, "123", "123");
// Save document
document.Save("HelloWorldWithSecurity.pdf");
#endregion
#region processing existing PDF and remove "print
high quality" permission
// Add new document
document = engine.AddDocument("HelloWorldWithSecurity.pdf");
// check if document is protected
if(document.Protected){
// check password
if ( document.ValidatePassword("123")==PasswordValidationType.Invalid){
Console.WriteLine("Invalid password");
}
else // removing protection
document.RemoveProtection();
}
// re-add protection with the same permissions except
"print high quality" permission
document.AddProtection(
document.Permissions ^ DocumentPermissionType.PrintHiResolution,
SecurityKeyLength._128Bit, "", "");
// Save document
document.Save("HelloWorldWithSecurity_HiPrintRemoved.pdf");
#endregion
}
}
}
Download example source code: bytescoutpdf_change_permissons_of_existing_pdf.zip
(10 KB)
For HTML2PDF and XML2PDF solution check our PDFDoc Scout ActiveX library instead