@asad.ali
Thank you. I have solved this in my own way. I have tested various pdf file, currently works fine.
Furthermore, I want to transfer each page to image and convert back to pdf in case anyone could select the content in pdf. I have checked what you shared in the previous post, and which image format you recommend most? I need relatively good quality and transfer in fast speed.
public byte[] AddWatermarksOnPdf(InputFileInfoExpDto input)
{
using (var pdfOrigin = new Aspose.Pdf.Document(input.FilePath))
using (var ms = new MemoryStream())
{
int fontSize = 20;
int lineSpacing = 10;
double rotateAngle = Math.PI / 4;
//Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})");
Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
new Aspose.Pdf.Facades.FontColor(128, 128, 128),
Aspose.Pdf.Facades.FontStyle.Unknown,
Aspose.Pdf.Facades.EncodingType.Identity_h,
true,
fontSize,
lineSpacing);
formattedText.AddNewLineText(input.Email, 10);
formattedText.AddNewLineText(input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss"), 10);
Aspose.Pdf.TextStamp stamp = new Aspose.Pdf.TextStamp(formattedText);
//stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
//stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
//stamp.TextAlignment = Aspose.Pdf.HorizontalAlignment.Left;
stamp.XIndent = 0;
stamp.YIndent = 0;
stamp.RotateAngle = 45;
stamp.Draw = true;
stamp.Background = false;
stamp.Opacity = 0.6;
//stamp.Width = 180;
//stamp.Height = 180;
//stamp.TextState.FontSize = 20;
//stamp.TextState.ForegroundColor = Aspose.Pdf.Color.Gray;
Aspose.Pdf.Facades.FormattedText formattedText1 = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
new Aspose.Pdf.Facades.FontColor(128, 128, 128),
Aspose.Pdf.Facades.FontStyle.Unknown,
Aspose.Pdf.Facades.EncodingType.Identity_h,
true,
fontSize,
lineSpacing);
Aspose.Pdf.Facades.FormattedText formattedText2 = new Aspose.Pdf.Facades.FormattedText($"{input.Email}",
new Aspose.Pdf.Facades.FontColor(128, 128, 128),
Aspose.Pdf.Facades.FontStyle.Unknown,
Aspose.Pdf.Facades.EncodingType.Identity_h,
true,
fontSize,
lineSpacing);
Aspose.Pdf.Facades.FormattedText formattedText3 = new Aspose.Pdf.Facades.FormattedText($"{input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss")}",
new Aspose.Pdf.Facades.FontColor(128, 128, 128),
Aspose.Pdf.Facades.FontStyle.Unknown,
Aspose.Pdf.Facades.EncodingType.Identity_h,
true,
fontSize,
lineSpacing);
var points = new List<PointF>();
points.Add(new PointF(0, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing));
points.Add(new PointF(formattedText1.TextWidth, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing));
points.Add(new PointF(formattedText1.TextWidth, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing + formattedText1.TextHeight));
points.Add(new PointF(0, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing + formattedText1.TextHeight));
points.Add(new PointF(0, formattedText3.TextHeight + lineSpacing));
points.Add(new PointF(formattedText2.TextWidth, formattedText3.TextHeight + lineSpacing));
points.Add(new PointF(formattedText2.TextWidth, formattedText3.TextHeight + lineSpacing + formattedText2.TextHeight));
points.Add(new PointF(0,formattedText3.TextHeight + lineSpacing + formattedText2.TextHeight));
points.Add(new PointF(0, 0));
points.Add(new PointF(formattedText3.TextWidth, 0));
points.Add(new PointF(formattedText3.TextWidth, formattedText3.TextHeight));
points.Add(new PointF(0, formattedText3.TextHeight));
var rotatePoints = new List<PointF>();
foreach (var point in points)
{
var rotatePoint = RotatePoint(point, rotateAngle);
rotatePoints.Add(rotatePoint);
}
var rectBox = new RectBoundingBox(rotatePoints);
double stampWidth = rectBox.Width;
double stampHeight = rectBox.Height;
double xGap = 10;
double yGap = 10;
foreach (var page in pdfOrigin.Pages)
{
var width = page.MediaBox.Width;
var height = page.MediaBox.Height;
int horizonCount = (int)(width / (stampWidth + xGap)) + 1;
int verticalCount = (int)(height / (stampHeight + yGap)) + 1;
for (int i = 0; i < horizonCount; i++)
{
for (int j = 0; j < verticalCount; j++)
{
stamp.XIndent = i * (stampWidth + xGap);
stamp.YIndent = j * (stampHeight + yGap);
page.AddStamp(stamp);
}
}
}
pdfOrigin.Save(ms);
return ms.ToArray();
}
}