have to assign/ protect a existing PDF file to password protected file using SSIS script task.
I have tried this code in windows forms(C#), its working fine but when i use this in SSIS script task it is throwing an Runtime error.
i have added itextsharp dll file to my script task reference.
can u please suggest the solution/ any changes in code that has to be made to use it in ssis script task.
Added itextsharp.dll to the script task reference folder.
namespaces that i have added.
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
code used in my ssis script task
public void Main()
{
// TODO: Add your code here
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "Test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "Test_enc.pdf");
using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, "test", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Below Run Time Error that is encountering through Script Task
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
What I have tried:
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
code used in my ssis script task
public void Main()
{
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "Test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "Test_enc.pdf");
using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, "test", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}