public static Byte[] AddContentWatermarkForPdf(Byte[] pdfBytes, WatermarkSetting watermarkSetting)
{
using (var pdfStream = new MemoryStream(pdfBytes))
using (var pdfDocument = new Aspose.Pdf.Document(pdfStream))
using (var singlePdfStream = new MemoryStream(GetSinglePdf(pdfBytes, watermarkSetting)))
using (var resultStream = new MemoryStream())
{
var pageCount = pdfDocument.Pages.Count;
var pdfStamp = new PdfPageStamp(singlePdfStream, 1);
pdfStamp.XIndent = 0;
pdfStamp.YIndent = 0;
pdfStamp.Opacity = watermarkSetting.Opacity;
for (int index = 1; index <= pageCount; index++)
{
pdfDocument.Pages[index].AddStamp(pdfStamp);
}
pdfDocument.Save(resultStream, SaveFormat.Pdf);
return resultStream.ToBytes();
}
}
private static Byte[] GetSinglePdf(Byte[] pdfBytes, WatermarkSetting watermarkSetting)
{
using (var stream = new MemoryStream(pdfBytes))
using (var pdfDocument = new Aspose.Pdf.Document(stream))
{
return GetSinglePdf(pdfDocument.Pages[1].MediaBox.Height, pdfDocument.Pages[1].MediaBox.Width, watermarkSetting);
}
}
private static Byte[] GetSinglePdf(Double height, Double width, WatermarkSetting watermarkSetting)
{
try
{
Aspose.Pdf.Text.FontRepository.FindFont(watermarkSetting.WatermarkFont);
}
catch
{
watermarkSetting.WatermarkFont = “Calibri”;
}
watermarkSetting.WatermarkFontSize = watermarkSetting.WatermarkFontSize.Replace(“pt”, “”).Replace(“Pt”, “”);
var fontSize = 48;
Int32.TryParse(watermarkSetting.WatermarkFontSize, out fontSize);
watermarkSetting.WatermarkFontSize = fontSize + “”;
using (var g = Graphics.FromImage(new Bitmap(800, 800)))
using (var ms = new MemoryStream())
{
g.PageUnit = GraphicsUnit.Pixel;
using (var transparentImg = new Bitmap((Int32)width, (Int32)height))
using (var transparentG = Graphics.FromImage(transparentImg))
using (var transparentPNGMS = new MemoryStream())
{
transparentG.Clear(System.Drawing.Color.Transparent);
transparentImg.Save(transparentPNGMS, ImageFormat.Png);
using (var middleStream = new MemoryStream(transparentPNGMS.ToArray()))
{
var PdfConvertorFactory = new PdfConvertorFactory();
using (var transparentPdfStream = PdfConvertorFactory.GetConvertor(“png”).Convert(middleStream))
using (var mmStream = new MemoryStream(transparentPdfStream.ToBytes()))
using (var singlePdf = new Aspose.Pdf.Document(mmStream))
using (var resultStream = new MemoryStream())
{
singlePdf.Background = Aspose.Pdf.Color.Transparent;
var formatedSingleText = formateSingleText(watermarkSetting);
var textStamp = new TextStamp(formatedSingleText);
SetSingleStamp(textStamp, watermarkSetting, width, height);
System.Drawing.Font _Font = new System.Drawing.Font(watermarkSetting.WatermarkFont, (float)fontSize, System.Drawing.FontStyle.Regular);
SizeF _SizeF = g.MeasureString(watermarkSetting.Watermark, _Font);
var textHeight = _SizeF.Height;
var textWidth = _SizeF.Width;
//add stamp to particular page
if (watermarkSetting.IsRepeated)
{
if (watermarkSetting.Direction == -90)
{
for (int i = 0; i < width / (textWidth - 25); i++)
{
for (int j = 0; j < height / (textHeight + 25); j++)
{
//setStamp(ts, entity);
textStamp.XIndent = j * (textHeight - 25) + 10;
textStamp.YIndent = i * (textWidth + 25);
singlePdf.Pages[1].AddStamp(textStamp);
}
}
}
else
{
for (int i = 0; i < height / (textHeight + 25); i++)
{
for (int j = 0; j < width / (textWidth - 25); j++)
{
//setStamp(ts, entity);
textStamp.XIndent = j * (textWidth - 25) + 10;
textStamp.YIndent = i * (textHeight + 25);
singlePdf.Pages[1].AddStamp(textStamp);
}
}
}
}
else
{
singlePdf.Pages[1].AddStamp(textStamp);
}
singlePdf.Save(resultStream, SaveFormat.Pdf);
return resultStream.ToArray();
}
}
}
}
}