@mudassir.fayyaz
This is the below code I have implemented in my POC and the annotated pdf showing proper text stamp.But while I have implemented the same in the project the text stamp not showing as i have mentioned above.
Its a urgnet requirement please give me the solution.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Border = Aspose.Pdf.Annotations.Border;
using Rectangle = Aspose.Pdf.Rectangle;
using Aspose.Pdf.Text;
using Aspose.Pdf.Facades;
using Newtonsoft.Json;
using System.Drawing;
using Microsoft.AspNetCore.Cors;
namespace PrintPDFWebApi.Controllers
{
[Route(“[controller]”)]
[ApiController]
public class PDFController : ControllerBase
{
private static readonly double defaultLeftIndent = 50;
private static readonly double defaultLineHeight = 18;
[HttpGet]
//public void CreatePDF()
[EnableCors("AllowOrigin")]
public void CreatePDF()
{
//"[2550,3299]" ImageSize from Documentoage table or pageHeight/pageWidth from datasetdocument table
var pageHeight = 3299;
var pageWidth = 2550;
//Human Annotation Values
var rectWidth = 1041;
var rectHeight = 57;
var xPosition = 759;
var yPosition = 249;
string unserName = "Name : Test User Name";
string attributeName = "Attribute Name : Test Attribute Name";
string commentText = "Comment : test comment";
Aspose.Pdf.Color Color = Aspose.Pdf.Color.LightGreen;
Aspose.Pdf.Color InteriorColor = Aspose.Pdf.Color.Yellow;
string categoryText = "B";
int textPosition = 1;
var rectWidth1 = 699;
var rectHeight1 = 59;
var xPosition1 = 927;
var yPosition1 = 400;
string unserName1 = "Test User Name1";
string attributeName1 = "Test Attribute Name1";
string commentText1 = "Comment : test comment1";
Aspose.Pdf.Color Color1 = Aspose.Pdf.Color.Blue;
Aspose.Pdf.Color InteriorColor1 = Aspose.Pdf.Color.Red;
string categoryText1 = "s";
int textPosition1 = 2;
var rectWidth2 = 174;
var rectHeight2 = 49;
var xPosition2 = 1238;
var yPosition2 = 1197;
string unserName2 = "Test User Name2";
string attributeName2 = "Test Attribute Name2";
string commentText2 = "Comment : test comment2";
Aspose.Pdf.Color Color2 = Aspose.Pdf.Color.Blue;
Aspose.Pdf.Color InteriorColor2 = Aspose.Pdf.Color.Green;
string categoryText2 = "S";
int textPosition2 = 2;
var rectWidth3 = 220;
var rectHeight3 = 49;
var xPosition3 = 1249;
var yPosition3 = 1049;
string unserName3 = "Test User Name3";
string attributeName3 = "Test Attribute Name3";
string commentText3 = "Comment : test comment3";
Aspose.Pdf.Color Color3 = Aspose.Pdf.Color.Black;
Aspose.Pdf.Color InteriorColor3 = Aspose.Pdf.Color.Gray;
string categoryText3 = "Z";
int textPosition3 = 1;
var rectWidth4 = 944;
var rectHeight4 = 350;
var xPosition4 = 1006;
var yPosition4 = 650;
string unserName4 = "Test User Name4";
string attributeName4 = "Test Attribute Name4";
string commentText4 = "Comment : test comment4";
Aspose.Pdf.Color Color4 = Aspose.Pdf.Color.Black;
Aspose.Pdf.Color InteriorColor4 = Aspose.Pdf.Color.Blue;
string categoryText4 = "D";
int textPosition4 = 2;
JsonToImage(); // Convert Json To Image
Document doc = new Document();
var files = new DirectoryInfo(@"Images/").GetFiles();
String dataDir = "F:/Pams/PrintPDFApi/PrintPDFWebApi/downloadedPDF/";
//foreach (var file in files)
//{
// int i = 0;
for (int i = 0; i < files.Length; i++)
{
if (i >= 4)
{
break;
}
var fileStream = files[i].ToString();
using (FileStream fs = new FileStream(fileStream, FileMode.Open, FileAccess.Read))
{
//#region Image
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileStream);
int h = srcImage.Height;
//Read Height of input image
int w = srcImage.Width;
//Initialize a new PDF document
//Add an empty page
Page page = doc.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = srcImage.ToString();
image.File = fileStream;
//Set page dimensions and margins
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
//#endregion
CreateAnnotationFromHumanAnnotation(doc, rectWidth, rectHeight, xPosition, yPosition, pageHeight, pageWidth, unserName, attributeName, Color, InteriorColor,commentText,categoryText, textPosition);
CreateAnnotationFromHumanAnnotation(doc, rectWidth1, rectHeight1, xPosition1, yPosition1, pageHeight, pageWidth, unserName1, attributeName1, Color1, InteriorColor1,commentText1, categoryText1, textPosition1);
CreateAnnotationFromHumanAnnotation(doc, rectWidth2, rectHeight2, xPosition2, yPosition2, pageHeight, pageWidth, unserName2, attributeName2, Color2, InteriorColor2,commentText2, categoryText2, textPosition2);
CreateAnnotationFromHumanAnnotation(doc, rectWidth3, rectHeight3, xPosition3, yPosition3, pageHeight, pageWidth, unserName3, attributeName3, Color3, InteriorColor3,commentText3, categoryText3, textPosition3);
CreateAnnotationFromHumanAnnotation(doc, rectWidth4, rectHeight4, xPosition4, yPosition4, pageHeight, pageWidth, unserName4, attributeName4, Color4, InteriorColor4,commentText4, categoryText4, textPosition4);
}
//Path.GetFileName(s);
doc.Save(dataDir + "doc.pdf");
// }
}
}
public void JsonToImage()
{
var files = new DirectoryInfo(@"JSONPdf/").GetFiles();
String dataDir = "F:/Pams/PrintPDFApi/PrintPDFWebApi/Images/";
foreach (var file in files)
{
var fileStream = file.ToString();
List<imageModel> model = JsonConvert.DeserializeObject<List<imageModel>>(System.IO.File.ReadAllText(fileStream));
int i = 0;
foreach (var item in model)
{
i++;
byte[] bytes = Convert.FromBase64String(item.pageImageBase64);
using (MemoryStream mst = new MemoryStream(Convert.FromBase64String(item.pageImageBase64)))
{
using (Bitmap bm2 = new Bitmap(mst))
{
bm2.Save(dataDir + "doc" + i + ".jpg");
}
}
}
}
}
private void GetRectCoordinateFromMachineAnnotation(Document doc, int bbox_top, int bbox_left, int bbox_right, int bbox_bottom, int pageHeight, int pageWidth, string unserName, string attributeName, Aspose.Pdf.Color color, Aspose.Pdf.Color interiorColor)
{
var LLX = bbox_left;
var LLY = pageHeight - bbox_bottom;
var URX = bbox_right;
var URY = pageHeight - bbox_top;
var squareAnnotation = new SquareAnnotation(doc.Pages[1], new Rectangle(LLX, LLY, URX, URY))
{
Title = "John Smith",
Color = color,
InteriorColor = interiorColor,
Opacity = 0.25,
//Popup = new PopupAnnotation(doc.Pages[1], new Rectangle(bbox_left + 800, pageHeight - 200 - bbox_bottom, bbox_right + 800, pageHeight - 100 - bbox_top))
};
doc.Pages[1].Annotations.Add(squareAnnotation);
// Text Annotation
TextAnnotation textAnnotation = new TextAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(LLX, URY, URX, URY + 500));
textAnnotation.Title = unserName;
//textAnnotation.Subject = "Sample Subject";
//textAnnotation.State = AnnotationState.Accepted;
textAnnotation.Contents = attributeName;
textAnnotation.Open = true;
textAnnotation.Icon = TextIcon.Comment;
Border border = new Border(textAnnotation);
border.Width = 1;
border.Dash = new Dash(1, 1);
textAnnotation.Border = border;
textAnnotation.Rect = new Aspose.Pdf.Rectangle(LLX, URY, URX, URY + 500);
doc.Pages[1].Annotations.Add(textAnnotation);
}
private void CreateAnnotationFromHumanAnnotation(Document doc, int rectWidth, int rectHeight, int xPosition, int yPosition, int pageHeight, int pageWidth, string unserName, string attributeName, Aspose.Pdf.Color color, Aspose.Pdf.Color interiorColor,string commentText, string txt, int position)
{
var bbox_top = yPosition;
var bbox_left = xPosition;
var bbox_right = xPosition + rectWidth;
var bbox_bottom = yPosition + rectHeight;
var LLX = bbox_left;
var LLY = pageHeight - bbox_bottom;
var URX = bbox_right;
var URY = pageHeight - bbox_top;
var squareAnnotation = new SquareAnnotation(doc.Pages[1], new Rectangle(LLX, LLY, URX, URY))
{
Title = "John Smith",
Color = color,
InteriorColor = interiorColor,
Opacity = 0.25,
//Popup = new PopupAnnotation(doc.Pages[1], new Rectangle(bbox_left + 800, pageHeight - 200 - bbox_bottom, bbox_right + 800, pageHeight - 100 - bbox_top))
};
doc.Pages[1].Annotations.Add(squareAnnotation);
// Added TimeStamp
if (position == 1)
{
FormattedText text = new FormattedText(txt);
TextStamp stamp = new TextStamp(text);
stamp.TextState.FontSize = 30.0F;
stamp.TextState.FontStyle = FontStyles.Bold;
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Aqua);
stamp.XIndent = LLX - 20;
stamp.YIndent = LLY + (rectHeight / 3);
doc.Pages[1].AddStamp(stamp);
}else if (position == 2)
{
FormattedText text1 = new FormattedText(txt);
TextStamp stamp1 = new TextStamp(text1);
stamp1.TextState.FontSize = 30.0F;
stamp1.TextState.FontStyle = FontStyles.Bold;
stamp1.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Aqua);
stamp1.XIndent = URX;
stamp1.YIndent = URY - (rectHeight / 3);
doc.Pages[1].AddStamp(stamp1);
}
// Text Annotation
TextAnnotation textAnnotation = new TextAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(LLX, URY, URX, URY + 500));
textAnnotation.Title = unserName;
textAnnotation.Contents = attributeName + commentText;
textAnnotation.Open = true;
textAnnotation.Icon = TextIcon.Key;
Border border = new Border(textAnnotation);
border.Width = 1;
border.Dash = new Dash(1, 1);
textAnnotation.Border = border;
textAnnotation.Rect = new Aspose.Pdf.Rectangle(LLX, URY, URX, URY + 500);
doc.Pages[1].Annotations.Add(textAnnotation);
}
public class imageModel
{
public int pid { get; set; }
public string pageImageBase64 { get; set; }
public string mediaType { get; set; }
}
}
}