none
How to pass and use variables to iTextSharp .dll used in script task

    Question

  • Hi,

    new user here, so howdy !

    So i try to explain what i’m trying to achieve here – pdf watermarking with itextsharp.

    Requirements:

    • Each PDF file needs a watermark in this catalog.
    • Each page of a pdf file needs a watermark.
    • Watermark needs to be the name of the file without the file extension (.PDF).
    • Watermark position fixed, say top-middle or bottom-middle of a page, pdf's are in A4 size always.
    • Pdf's must keep their original file names and stay in the same folder "c:\ftp_out\test" after watermarking.

    I must add i'm not a programmer so bear with me. But i'm learning :)

    At one point i created a forum topic and asked for help:

    http://stackoverflow.com/questions/13741160/watermark-existing-pdfs-before-ftping-them-in-visual-studio-2008

    So i pretty much implemented everything there but as i ’m not programmer i don’t know how to define and use variables in this script.

    Created a package variable „Pdf_To_Stamp“, created a for each loop container with a following collection, added the variable i mentioned earlier and gave initial value of 0 under "variable mappings"

    PDF's are in the catalog "c:\ftp_out\test", "Files" value is "*.PDF", Fully qualified. No other settings for "foreach loop"

    Inside of the foreach loop i placed a script task, readonlyvariables i put the "User::Pdf_To_Stamp" i mentioned earlier. the script it self is following:

    using System;
    using System.IO;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    using iTextSharp.text;
    using iTextSharp.text.pdf;


    namespace ST_f19c41b37b1247068cf0b6f35d026cfa.csproj
    {
        [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
        public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        {


            public static byte[] Stamp(byte[] resource)
            {
                PdfReader reader = new PdfReader(resource);
                using (var ms = new MemoryStream())
                {
                    using (PdfStamper stamper = new PdfStamper(reader, ms))
                    {
                        PdfContentByte canvas = stamper.GetOverContent(1);
                        ColumnText.ShowTextAligned(
                          canvas,
                          Element.ALIGN_LEFT,
                          new Phrase("name of the pdf file without the extension (.PDF) needed "),
                          36, 540, 0
                        );
                    }
                    return ms.ToArray();
                }
            }
        }
    }

    But as you guessed correctly, it's not working :) So i'm clearly missing something here. I'm missing variables is my guess :)

    So i'm kindly asking for your help to solve this for me
    Saturday, January 05, 2013 9:32 AM

Answers

  • Thank you for answering but unfortunately i don't understand where to put that, can you please post the whole code section how it should look like?

    I think also this part of the code needs some chneges:

    new Phrase("name of the pdf file without the extension (.PDF) needed "),

    This isn't a SSIS related question. You should ask this in a C# forum or in a forum from/about iTextSharp. If you have no .Net experience at all then you should probably ask a colleague who does or hire someone.


    You should add a reference to the iTextSharp api and then you could would probably look somthing like (not tested, no guarantees):

    using System;
     using System.IO;
     using System.Data;
     using Microsoft.SqlServer.Dts.Runtime;
     using System.Windows.Forms;
     using iTextSharp.text;
     using iTextSharp.text.pdf;
     
    
    namespace ST_f19c41b37b1247068cf0b6f35d026cfa.csproj
    {
         [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
         public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
         {
     
              public void Main() 
              {
                  // you should change this from a hardcoded path to a 
                  // variable or connection manager
                  string filePath = "c:\file.pdf"
    
                  byte[] byteArray1 = System.IO.File.ReadAllBytes(filePath);
    
    
                  byte[] byteArray2 = Stamp(byteArray1);
    
                  File.WriteAllBytes(filePath, byteArray2);
    
              }
    
              public static byte[] Stamp(byte[] resource)
              {
                 PdfReader reader = new PdfReader(resource);
                 using (var ms = new MemoryStream())
                 {
                     using (PdfStamper stamper = new PdfStamper(reader, ms))
                     {
                         PdfContentByte canvas = stamper.GetOverContent(1);
                         ColumnText.ShowTextAligned(
                           canvas,
                           Element.ALIGN_LEFT,
                           new Phrase("name of the pdf file without the extension (.PDF) needed "),
                           36, 540, 0
                         );
                     }
                     return ms.ToArray();
                 }
             }
         }
     }



    Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter

    • Marked as answer by marttali Saturday, January 05, 2013 1:42 PM
    Saturday, January 05, 2013 1:06 PM

All replies

  • You have created a method, but you're not executing it. The Script Task also has a main method. In that method you should call this new method.

    public void Main() 
    {
        Stamp(XXXXXXXXXXXXX);
    }

    Are you getting any built errors?


    Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter

    Saturday, January 05, 2013 11:14 AM
  • Thank you for answering but unfortunately i don't understand where to put that, can you please post the whole code section how it should look like?

    I think also this part of the code needs some chneges:

    new Phrase("name of the pdf file without the extension (.PDF) needed "),

    Saturday, January 05, 2013 12:38 PM
  • Thank you for answering but unfortunately i don't understand where to put that, can you please post the whole code section how it should look like?

    I think also this part of the code needs some chneges:

    new Phrase("name of the pdf file without the extension (.PDF) needed "),

    This isn't a SSIS related question. You should ask this in a C# forum or in a forum from/about iTextSharp. If you have no .Net experience at all then you should probably ask a colleague who does or hire someone.


    You should add a reference to the iTextSharp api and then you could would probably look somthing like (not tested, no guarantees):

    using System;
     using System.IO;
     using System.Data;
     using Microsoft.SqlServer.Dts.Runtime;
     using System.Windows.Forms;
     using iTextSharp.text;
     using iTextSharp.text.pdf;
     
    
    namespace ST_f19c41b37b1247068cf0b6f35d026cfa.csproj
    {
         [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
         public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
         {
     
              public void Main() 
              {
                  // you should change this from a hardcoded path to a 
                  // variable or connection manager
                  string filePath = "c:\file.pdf"
    
                  byte[] byteArray1 = System.IO.File.ReadAllBytes(filePath);
    
    
                  byte[] byteArray2 = Stamp(byteArray1);
    
                  File.WriteAllBytes(filePath, byteArray2);
    
              }
    
              public static byte[] Stamp(byte[] resource)
              {
                 PdfReader reader = new PdfReader(resource);
                 using (var ms = new MemoryStream())
                 {
                     using (PdfStamper stamper = new PdfStamper(reader, ms))
                     {
                         PdfContentByte canvas = stamper.GetOverContent(1);
                         ColumnText.ShowTextAligned(
                           canvas,
                           Element.ALIGN_LEFT,
                           new Phrase("name of the pdf file without the extension (.PDF) needed "),
                           36, 540, 0
                         );
                     }
                     return ms.ToArray();
                 }
             }
         }
     }



    Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter

    • Marked as answer by marttali Saturday, January 05, 2013 1:42 PM
    Saturday, January 05, 2013 1:06 PM
  • Thank you for your help. I will ask the rest from the c# forum.

    And will learn some basic programming as well :) There's clearly a need...

    Saturday, January 05, 2013 1:42 PM