Skip to content

Instantly share code, notes, and snippets.

@VipulShetkar
Last active August 11, 2022 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VipulShetkar/66f39554c01c83ca8a333e8f633a306b to your computer and use it in GitHub Desktop.
Save VipulShetkar/66f39554c01c83ca8a333e8f633a306b to your computer and use it in GitHub Desktop.
WatermarkPdf
using iText.IO.Font;
using iText.IO.Font.Constants;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
namespace Shared.Utility
{
public class PDFUtility
{
//
// Summary:
// Text Watermark in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkText:
// Text for watermarking.
//
// x:
// x co-ordinate for placing watermark Text.
//
// y:
// y co-ordinate for placing watermark Text.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdfWithText(byte[] sourceData,string watermarkText,float x,float y)
{
return WatermarkPdf(sourceData, null, watermarkText, null, null, null, x, y, null, null, null);
}
//
// Summary:
// Text Watermark in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkText:
// Text for watermarking.
//
// x:
// x co-ordinate for placing watermark Text.
//
// y:
// y co-ordinate for placing watermark Text.
//
// textFont:
// Name of Font or its location on file.
//
// fontSize:
// Font size of watermark Text.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdfWithText(byte[] sourceData, string watermarkText, float x, float y, string textFont, int? fontSize)
{
return WatermarkPdf(sourceData, null, watermarkText, textFont, fontSize, null, x, y, null, null, null);
}
//
// Summary:
// Text Watermark in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkText:
// Text for watermarking.
//
// x:
// x co-ordinate for placing watermark Text.
//
// y:
// y co-ordinate for placing watermark Text.
//
// textFont:
// Name of Font or its location on file.
//
// fontSize:
// Font size of watermark Text.
//
// radAngle:
// the angle of rotation applied to the text, in radians.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdfWithText(byte[] sourceData, string watermarkText, float x, float y, string textFont, int? fontSize, float? radAngle)
{
return WatermarkPdf(sourceData, null, watermarkText, textFont, fontSize, null, x, y, null, null, radAngle);
}
//
// Summary:
// Text Watermark in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkText:
// Text for watermarking.
//
// x:
// x co-ordinate for placing watermark Text.
//
// y:
// y co-ordinate for placing watermark Text.
//
// textFont:
// Name of Font or its location on file.
//
// fontSize:
// Font size of watermark Text.
//
// textAlign:
// horizontal alignment about the specified point
//
// vertAlign:
// vertical alignment about the specified point
//
// radAngle:
// the angle of rotation applied to the text, in radians.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdfWithText(byte[] sourceData, string watermarkText, float x, float y, string textFont, int? fontSize,
TextAlignment? textAlign, VerticalAlignment? vertAlign, float? radAngle)
{
return WatermarkPdf(sourceData, null, watermarkText, textFont, fontSize, null, x, y, textAlign, vertAlign, radAngle);
}
//
// Summary:
// Watermark Image in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkImgPath:
// Image path for watermarking.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdfWithImage(byte[] sourceData, string watermarkImgPath)
{
return WatermarkPdf(sourceData, watermarkImgPath, string.Empty, string.Empty, null, null, null, null, null, null, null);
}
//
// Summary:
// Watermark Image in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkImgPath:
// Image path for watermarking.
//
// opacity:
// x co-ordinate for placing watermark Text.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdfWithImage(byte[] sourceData, string watermarkImgPath, float? opacity)
{
return WatermarkPdf(sourceData, watermarkImgPath, string.Empty, string.Empty, null, opacity, null, null, null, null, null);
}
//
// Summary:
// Text Watermark in pdf document
//
// Parameters:
// sourceData:
// pdf document as byte[].
//
// watermarkImgPath:
// pdf document as byte[].
//
// watermarkText:
// Text for watermarking.
//
// textFont:
// Name of Font or its location on file.
//
// fontSize:
// Font size of watermark Text.
//
// opacity:
// x co-ordinate for placing watermark Text.
//
// x:
// x co-ordinate for placing watermark Text.
//
// y:
// y co-ordinate for placing watermark Text.
//
// textAlign:
// horizontal alignment about the specified point
//
// vertAlign:
// vertical alignment about the specified point
//
// radAngle:
// the angle of rotation applied to the text, in radians.
//
// Remarks:
// Text watermark in pdf document.
public static byte[] WatermarkPdf(
byte[] sourceData,
string watermarkImgPath,
string watermarkText,
string textFont,
int? fontSize,
float? opacity,
float? x,
float? y,
TextAlignment? textAlign,
VerticalAlignment? vertAlign,
float? radAngle
)
{
using (var ms = new MemoryStream())
{
var sourceStream = new MemoryStream(sourceData);
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceStream), new PdfWriter(ms));
Document doc = new Document(pdfDoc);
PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(string.IsNullOrEmpty(textFont)?StandardFonts.TIMES_ROMAN:textFont));
Paragraph paragraph = new Paragraph(new Text(watermarkText)).SetFont(font).SetFontSize(fontSize ?? 7);
// Loop through all pages for adding watermark text/image at all pages of pdf
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
PdfPage pdfPage = pdfDoc.GetPage(i);
Rectangle pageSize = pdfPage.GetPageSize();
float pg_x = (pageSize.GetLeft() + pageSize.GetRight()) / 2;
float pg_y = (pageSize.GetTop() + pageSize.GetBottom()) / 2;
doc.ShowTextAligned(paragraph, x ?? pg_x, y ?? pg_y, i, textAlign?? TextAlignment.LEFT, vertAlign?? VerticalAlignment.TOP, radAngle ?? 0);
if (!string.IsNullOrEmpty(watermarkImgPath))
{
PdfCanvas over = new PdfCanvas(pdfPage);
over.SaveState();
PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(opacity ?? 0.5f);
over.SetExtGState(gs1);
ImageData img = ImageDataFactory.Create(watermarkImgPath);
float w = img.GetWidth();
float h = img.GetHeight();
over.AddImageWithTransformationMatrix(img, w, 0, 0, h, pg_x - (w / 2), pg_y - (h / 2), true);
over.RestoreState();
}
}
doc.Close();
return ms.ToArray();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment