Friday, 16 August 2013

How to: Add Watermark to PDFs Programmatically using iTextSharp

Being able to add text or image watermark to PDF files is one of those handy things you can archive via program.

The only library required for the little program is iTextSharp. For those who haven't used it yet, iTextSharp is a C# version of iText, which allows you to generate or manipulate PDFs.

So let's cut the chitchat, and go straight to the code. By calling 'AddTextWatermark' or 'AddImageWatermark' function with required parameters supplied, you are ready to rock!


public void CreateTemplate(string watermarkText, string targetFileName){
    var document = new Document();
    var pdfWriter = PdfWriter.GetInstance(document, new FileStream(targetFileName, FileMode.Create));
    var font = new Font(Font.FontFamily.HELVETICA, 60, Font.NORMAL, BaseColor.LIGHT_GRAY);
    document.Open();
    // Specify font and alignment of text watermark
    ColumnText.ShowTextAligned(pdfWriter.DirectContent, Element.ALIGN_CENTER, new Phrase(watermarkText, font), 300, 400, 45);
    document.Close();
}

// Add watermark using a PDF file
public void AddTextWatermark(string sourceFilePath, string watermarkTemplatePath, string targetFilePath){
    var pdfReaderSource = new PdfReader(sourceFilePath);
    var pdfStamper = new PdfStamper(pdfReaderSource, new FileStream(targetFilePath, FileMode.Create));
    var pdfReaderTemplate = new PdfReader(watermarkTemplatePath);
    var page = pdfStamper.GetImportedPage(pdfReaderTemplate, 1);

    for (var i = 0; i < pdfReaderSource.NumberOfPages; i++)
    {
        var content = pdfStamper.GetUnderContent(i + 1);
        content.AddTemplate(page, 0, 0);
    }

    pdfStamper.Close();
    pdfReaderTemplate.Close();
}

// Add watermark to a file using an image
public void AddImageWatermark(string sourceFilePath, string watermarkImagePath, string targetFilePath){
    var pdfReader = new PdfReader(sourceFilePath);
    var pdfStamper = new PdfStamper(pdfReader, new FileStream(targetFilePath, FileMode.Create));
    var image = Image.GetInstance(watermarkImagePath);
    image.SetAbsolutePosition(200, 400);

    for (var i = 0; i < pdfReader.NumberOfPages; i++)
    {
        var content = pdfStamper.GetUnderContent(i + 1);
        content.AddImage(image);
    }

    pdfStamper.Close();
}

Tuesday, 13 August 2013

How to: Setup FTP Server on Windows Server 2008 R2 and Use FTP Programmatically

• Setup a FTP server on Windows Server 2008 R2

Open 'Server Manager', and add new 'Role' to it.

 In 'Add Roles Wizard', tick 'Web Server (IIS)'.
In 'Role Services', tick everything under 'FTP Server' section. Follow the wizard to the end to install all the features required.
Open IIS, tick 'Add FTP Site' when right-click on 'Sites' to add a new FTP site.
Fill in the desirable site name and file location.
Choose 'All Unassigned' for 'IP Address', and do not SSL if you just want a simple FTP site.
Choose 'Basic Authentication' and allow 'All Users' to 'Read' and 'Write'. And carry on finishing the 'Add FTP Site' wizard.
Open 'Windows Firewall with Advanced Security' to add a new rule to the FTP site.
Specify a port for FTP communication, and leave the rest as default.

To verify the FTP is working, navigate to 'ftp://<yourftpsiteaddress>' in your browser.

• FTP client

//using System;
//using System.IO;
//using System.Net;
//using System.Text;
void Main(){
   ListFtpServerDirectoryContents
();
  
UploadToFtpServer();
  
ListFtpServerDirectoryContents();
  
DownloadFromFtpServer();   }
public void ListFtpServerDirectoryContents() {
   var request
= (FtpWebRequest)WebRequest.Create("ftp://ftpserveraddresss/");
  
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
 
  
request.Credentials = new NetworkCredential("myusername", "mypassword");
 
  
var response = (FtpWebResponse)request.GetResponse();
 
  
var responseStream = response.GetResponseStream();
  
var reader = new StreamReader(responseStream);
  
Console.WriteLine(reader.ReadToEnd());
 
  
Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
 
  
reader.Close();
  
response.Close(); }
public void UploadToFtpServer(){
   var request
= (FtpWebRequest)WebRequest.Create("ftp://ftpserveraddresss/targetfilename");
  
request.Method = WebRequestMethods.Ftp.UploadFile;
 
  
request.Credentials = new NetworkCredential("myusername", "mypassword");
 
  
var sourceStream = new StreamReader("filepathhere");
  
var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  
sourceStream.Close();
  
request.ContentLength = fileContents.Length;
 
  
Stream requestStream = request.GetRequestStream();
  
requestStream.Write(fileContents, 0, fileContents.Length);
  
requestStream.Close();
 
  
var response = (FtpWebResponse)request.GetResponse();
 
  
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
 
  
response.Close(); }
public void DownloadFromFtpServer(){
   var request
= (FtpWebRequest)WebRequest.Create("ftp://ftpserveraddresss/targetfilename");
  
request.Method = WebRequestMethods.Ftp.DownloadFile;
 
  
request.Credentials = new NetworkCredential("myusername", "mypassword");
 
  
var response = (FtpWebResponse)request.GetResponse();
 
  
var responseStream = response.GetResponseStream();
  
var reader = new StreamReader(responseStream);
  
Console.WriteLine(reader.ReadToEnd());
 
  
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
 
  
reader.Close();
  
response.Close(); }