Search This Blog

Thursday, February 3, 2011

Adding Watermark, Pedigree and Overlays to the Pdf documents in SharePoint using iTextSharp

Many times we are required to add Watermark text, Pedigree and Overlays to the Pdf documents in sharePoint. There comes iTextSharp. I am sharing the code for generating these using iTextSharp dll. You can call this method "AddWatermark" in event handler of a list on pdf document add.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;
using System.Xml;
using System.IO;
using Microsoft.Office.Word.Server.Conversions;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Xml.Serialization;

namespace Watermarking
{
public partial class ABC : XYZ
{
internal static int numberOfPages;
internal static int pagecnt;


public void AddWaterMark(SPListItem objSPListItem)
{
byte[] buffer;

buffer = objSPListItem.File.OpenBinary();
PdfReader pdfReader = new PdfReader(buffer);

string strTextToDisplay = "Draft By Me";

var pageSize = pdfReader.GetPageSizeWithRotation(1);
int PageWidth = Convert.ToInt32(pageSize.Width);
int PageHeight = Convert.ToInt32(pageSize.Height);

numberOfPages = pdfReader.NumberOfPages;
PdfContentByte objPdfContentByte;

float fontSize = 90;

float xPosition = PageWidth / 2;
float yPosition = PageHeight / 2;

float angle = 35;

Document doc = new Document(PageSize.A4);

pdfPage page2 = new pdfPage();

PdfWriter docWriter = PdfWriter.GetInstance(doc, new FileStream("C:\\Temp\\test.pdf", FileMode.Create));

docWriter.PageEvent = page2;
doc.Open();
try
{
objPdfContentByte = docWriter.DirectContentUnder;

for (int i = 1; i <= numberOfPages; i++)
{
++pagecnt;
PdfImportedPage page = docWriter.GetImportedPage(pdfReader, i);
objPdfContentByte.AddTemplate(page, -5, -5);

BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);

objPdfContentByte.BeginText();
objPdfContentByte.SetTextRenderingMode(PdfContentByte.ALIGN_CENTER);

objPdfContentByte.SetFontAndSize(baseFont, fontSize);
ColumnText objColumnText = new ColumnText(objPdfContentByte);
objPdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, strTextToDisplay, xPosition, yPosition, angle);


objPdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
objPdfContentByte.SetColorFill(iTextSharp.text.Color.BLUE);
objPdfContentByte.SetFontAndSize(baseFont, 8);
objPdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, System.DateTime.Now.ToString(), 10, yPosition, 90);

objPdfContentByte.EndText();
doc.NewPage();
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
doc.Close();
}
}

public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{

protected iTextSharp.text.Font footer
{
get
{
iTextSharp.text.Font font = FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.GRAY);
return font;
}
}
protected iTextSharp.text.Font header
{
get
{
iTextSharp.text.Font font = FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLUE);
return font;
}
}

public override void OnStartPage(PdfWriter writer, Document doc)
{
float[] colsWidth = { 100f, 400f };
PdfPTable table = new PdfPTable(colsWidth);
table.SetWidthPercentage(colsWidth, doc.PageSize);
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell("Title:");
table.AddCell("Add Product");
table.AddCell("Created By:");
table.AddCell("abc");
table.AddCell("Last Modified:");
table.AddCell(System.DateTime.Now.ToString());
doc.Add(table);
}


public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfPTable footerTbl = new PdfPTable(2);
footerTbl.TotalWidth = doc.PageSize.Width - 90;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
Paragraph para = new Paragraph("Footer 1", footer);
para.Add(Environment.NewLine);
para.Add("Footer 2");
PdfPCell cell = new PdfPCell(para);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);

int pageno = Form1.pagecnt;
int Totalpage = Form1.numberOfPages;

para = new Paragraph("Page " + pageno + " of " + Totalpage, footer);
cell = new PdfPCell(para);
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
cell.Border = 0;
cell.PaddingRight = 10;
footerTbl.AddCell(cell);
footerTbl.WriteSelectedRows(0, -1, 40, (doc.BottomMargin + 10), writer.DirectContent);
}
}
}

Thanks

No comments:

Post a Comment