The following code will add watermark text to pdf document
You need to add the following headers to your aspx page
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
The following method will add watermark text
public static byte[] WriteToPdf(FileInfo sourceFile, string stringToWriteToPdf)
{
PdfReader reader = new PdfReader(sourceFile.FullName);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfStamper pdfStamper = new PdfStamper(reader, memoryStream);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
pdfPageContents.BeginText();
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
pdfPageContents.SetFontAndSize(baseFont,80);
pdfPageContents.SetRGBColorFill(137, 142, 136);
float textAngle =(float) GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stringToWriteToPdf, pageSize.Width/2, pageSize.Height/2, textAngle);
pdfPageContents.EndText();
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
reader.Close();
return memoryStream.ToArray();
}
}
The following method will set the angle for the watermark text
public static double GetHypotenuseAngleInDegreesFrom(double opposite, double adjacent)
{
double radians = Math.Atan2(opposite, adjacent);
double angle = radians*(180/Math.PI);
return angle;
}
Output :

You need to add the following headers to your aspx page
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
The following method will add watermark text
public static byte[] WriteToPdf(FileInfo sourceFile, string stringToWriteToPdf)
{
PdfReader reader = new PdfReader(sourceFile.FullName);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfStamper pdfStamper = new PdfStamper(reader, memoryStream);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
pdfPageContents.BeginText();
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
pdfPageContents.SetFontAndSize(baseFont,80);
pdfPageContents.SetRGBColorFill(137, 142, 136);
float textAngle =(float) GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stringToWriteToPdf, pageSize.Width/2, pageSize.Height/2, textAngle);
pdfPageContents.EndText();
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
reader.Close();
return memoryStream.ToArray();
}
}
The following method will set the angle for the watermark text
public static double GetHypotenuseAngleInDegreesFrom(double opposite, double adjacent)
{
double radians = Math.Atan2(opposite, adjacent);
double angle = radians*(180/Math.PI);
return angle;
}
Output :
Hello,
ReplyDeleteA-PDF watermark is a fast desktop utility program that lets you add watermarks to a batch of Acrobat PDF documents. You can also print the watermarked file without saving, and even monitor a hot directory, when files are copied in, they will be added watermark and sent to another directory automatically. Thanks...
Copy Protect PDF
Add Watermark to PDF document in ASP.NET, you can modify the rotation of the watermark text, font and text color can be also customized.
ReplyDelete