Required new type of Annotation

annotation.jpeg (47.2 KB)

Hello Team,
In my project I have a new requirement of implementing a new kind of annotation where we have to add a box annotation along with the Text.

Please find the attachment where clearly describes the requirement.

Regards,
Pramod

@pramodsahu

There is a lot of text in your snapshot. Do you mean the text “B” on the right side of the box or some other text with the blue box?

@mudassir.fayyaz
Thanks for your reply.
My requirement is the text “B” on the right side with the Box.

@pramodsahu

You can create a rectangle with any fill color and add text stamp at any location of page.

Working with Graphs in PDF file with C#
Add Text stamps in PDF programmatically

@mudassir.fayyaz
Let my try as per above.

Thanks

@pramodsahu

Sure. Please try and then share your feedback.

@mudassir.fayyaz

Thanks for the support.
I have implemented as followed by the above given links and also created the the POC for my project its working fine foe me as below
image.png (33.0 KB)

But the same code while i am using in the real project I don’t see text stamp. However, if I copy the TextStamp area in PDF document and paste into notepad, I can see the text.
I am using Aspose.Pdf 21.9.0

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);

image.png (169 Bytes)

FYI i also made the Background = false;

But still facing the same blank screen.

@pramodsahu

It’s good to know that shared articles were helpful. I request you to share a sample application for our investigations.

@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; }
    }
}

}

@pramodsahu

I request you to share a sample application as ZIP because your code looks to be using some sample files as well.

@mudassir.fayyaz
I can not able to share the whole application but below is my sample code which i have used in my application.

string dataDir = “F:/Pams/PrintPDFApi/PrintPDFWebApi/AnnotionPDF/”;
FormattedText text = new FormattedText(“B”);
Document doc = new Document(dataDir + “document.pdf”);
TextStamp stamp = new TextStamp(text);
stamp.TextState.FontSize = 50.0F;
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
stamp.Background = false;
stamp.XIndent = 500;
stamp.YIndent = 600;
stamp.TextState.FontStyle = FontStyles.Bold;
stamp.TextState.RenderingMode = TextRenderingMode.FillThenStrokeText;
dataDir = dataDir + “annotateddocument.pdf”;
doc.Save(dataDir);

document that i have used is below
document.pdf (2.0 MB)

@pramodsahu

Please add this line to your code before saving the document and share your feedback.

doc.Pages[1].AddStamp(stamp);

I have added the same but still facing the same issue

@mudassir.fayyaz
In your forum I could not able to upload the whole project in ZIP because the ZIP folder size is more than 50 MB.
So I have uploaded the controller where I have created all along with the JSON file.

1)Create a .Net Core project and create a controller.
2)Create a folder in the solution name it as “JSONPdf” and put the attached json file in the folder.
2)Create 2 more folder name as “Images” where the converted JSON to Images file will be store and folder “downloadedPDF” where the annotated file will be store.
The add the below running code inside the controller and check.
Json File
document.zip (2.5 MB)

Controller:
PDFController.zip (3.2 KB)

Structure
Captured.PNG (24.3 KB)

Output what I got which is not required
Screenshot.png (118.6 KB)

My expected result should be
Expected.PNG (35.4 KB)

Please find all and provide me the proper solution>

Expecting the good result.

@pramodsahu

Please share the ZIP file using Google Drive, One Drive or any file sharing service.

@mudassir.fayyaz
I have uploaded the Project Please find the same and give me the solution.

PrintPDFWebApiNew.zip (4.7 MB)

@pramodsahu

The text is added on the page but it goes behind the image. I suggest you to try Add Image stamps in PDF approach for adding images or use Free Text Annotation and share your feedback.