Convert mouse coordinates to PDF units using Aspose.PDF for .NET

@yogeshvjadhav

Would you please confirm if you have tried adding stamp in PDF using Aspose.PDF for .NET Library in a separate program with same parameters.

In case stamp is not appearing at desired position even using Aspose.PDF for .NET separately, please share the sample image file which you are using for stamp. We will test the scenario in our environment and address it accordingly.

signature.png (656 Bytes)

Please refer attached stamp.
Please do testing using shared document.

Following are the co-ordinates from HTML canvas

Ratio: 0.606060606060606
X-Axis: 368.7624750499
Y-Axis: 335.131782945737
Width:200
Height:60

And Actual converted co-ordinates while putting on document are

double shapeX = (shapes[shapeIndex].x * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 292.05988023952085

double shapeY = (shapes[shapeIndex].y * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 265.42437209302369

double shapeW = (shapes[shapeIndex].w * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 158.40000000000003

double shapeH = (shapes[shapeIndex].h * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 47.52000000000001

double yaxis = (doc.Pages[shapes[shapeIndex].p].Rect.URY - (shapeH + shapeY)); // 479.05562790697627

I have attached both working and not working document,
Please refer these document while testing.
NotWorkingForThisDocument.pdf (1.3 MB)

WorkingForThisDocument.pdf (287.5 KB)

Thanks,
Yogesh Jadhav

@yogeshvjadhav

We have added an image stamp using above parameters and following code snippet in our environment. For your kind reference, an output PDF is also attached.

string sImageUrl = dataDir + "signature.png";
Document pdfDoc = new Document(dataDir + "86_ebrochure.pdf");
ImageStamp imageStamp = new ImageStamp(sImageUrl);
imageStamp.Height = 60;
imageStamp.Width = 200;
imageStamp.XIndent = 368.7624750499;
imageStamp.YIndent = 335.131782945737;
//add header on all pages
foreach (Page opage in pdfDoc.Pages)
  opage.AddStamp(imageStamp);
//save updated document
pdfDoc.Save(dataDir + "86_ebrochure_output.pdf");

86_ebrochure_output.pdf (1.3 MB)

Would you please check the output and point out the desired location where you want to display the stamp by sharing a screenshot with some annotations. We will try to add stamp over desired location inside PDF document and share our feedback with you.

What should be the correct formula to calculate x, y, h and w if document is rotated on90, on180 and on270 ?

Thanks,
Yogesh Jadhav

This formula work if the document rotation None. But if the document rotation is on90, on180 and on270 its not work.

@yogeshvjadhav

Would you please share the definition/origin of shapes[shapeIndex]. We will test the scenario in our environment and address it accordingly.

shapes = [{x:154.191616766467,y:709.162790697675, h:60.0,w:200.0,ratio:“0.606060606060606”}]

x: X Position on web ui
y: Y position on web ui
h: hight of the image
w: width of the image
ratio: document page ratio

Following attached document is on90 rotation document.
86_ebrochure.pdf (1.3 MB)

Image for actual position on web ui.
Image1.png (517.4 KB)

The above formula is work if document rotation is none for others(90,180 and 270 rotation) its not work.

@yogeshvjadhav

Thanks for sharing more details.

We have tested the scenario in our environment and noticed that specified coordinates are not working when pages are rotated inside PDF. We also tried to change 1st page rotation to ‘None’ and observed that the image stamp got rendered at right coordinates.

//var shapes = [{ x: "154.191616766467",y: "709.162790697675", h: "60.0",w: "200.0",ratio:"0.606060606060606"}];
double shapeX = (154.191616766467 * 72 / 150) / Convert.ToDouble(0.606060606060606); 
double shapeY = (709.162790697675 * 72 / 150) / Convert.ToDouble(0.606060606060606); 
double shapeW = (200.0 * 72 / 150) / Convert.ToDouble(0.606060606060606); 
double shapeH = (60.0 * 72 / 150) / Convert.ToDouble(0.606060606060606); 
string sImageUrl = dataDir + "Image1.png";
Document pdfDoc = new Document(dataDir + "86_ebrochure.pdf");
//var rotation = pdfDoc.Pages[1].Rotate = Rotation.None; // In order to display the image stamp on desired coordinates
ImageStamp imageStamp = new ImageStamp(sImageUrl);
imageStamp.Height = shapeH;
imageStamp.Width = shapeW;
imageStamp.XIndent = shapeX;
imageStamp.YIndent = shapeY;
foreach (Page opage in pdfDoc.Pages)
 opage.AddStamp(imageStamp);
//save updated document
pdfDoc.Save(dataDir + "86_ebrochure_output.pdf");

86_ebrochure_output.pdf (1.5 MB)

It seems that specifying coordinates of image stamp is not working when pages are rotated. We have logged this issue as PDFNET-45348 in our issue tracking system for further investigation. As soon as we have some definite updates regarding resolution of the issue, we will surely inform you. Please be patient and spare us little time.

We are sorry for the inconvenience.

I am not able to download attached document.

@yogeshvjadhav

Please refer to following link in order to download or view file which was produced at our side while testing the scenario.

How much time will required to fix this issue? It’s very urgent for us.

@yogeshvjadhav

Thanks for your inquiry.

The issue is logged under normal/free support model where issues get resolved on first come first serve basis and have low priority. I regretfully share that your issue may take months to get resolved depending upon how long is the queue of previously logged issue.

In case your issue is urgent, we would like to recommend you considering our paid support option where issues have high priority and are resolved on urgent basis.

Any update on this one? I basically just needed to add text to a pdf (which is originally converted from image). I have the X & Y location (in pixels) where I would want the text to be added. I tried using the formula above but didn’t work for me either. I needed to add the text to the pdf instead of image to make it searchable.

At the moment, we are using an older version of Aspose.PDF and wanted to make sure if it’s worth an upgrade.

@rfernando

In case you have X,Y positions of text to be added, you can please add text stamp by specifying them in PDF document. In case it does not help, please share your sample PDF document with us along with X,Y positions and an expected output PDF. We will test the scenario in our environment and address it accordingly.

Hi Asad,

Thank you for responding.

I tried using add text stamp as per your suggestion but like the add text fragment that I tried, the text is added in a different location. I understand that you are using PDF points for both textFragment.Position or textStamp.XIndent and textStamp.YIndent but I have the position in pixels.

I have attached the following image and the converted Image to PDF output.
form.png (159.8 KB)
ImageToPDF_out.pdf (207.2 KB)

The location of one of the texts in PIXELS is X=389 and Y=23. The text should be populated after the Name: label you see in the form.

Hope to hear from you soon.

Kind regards,
Ruby

@rfernando

We tried to add the text stamp by converting pixels into points using following code snippet but, could not get correct results:

var pdf = new Document(dataDir + "ImageToPDF_out.pdf");
var page = pdf.Pages[1];
var stamp = new TextStamp("Name");
stamp.Background = false;
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
stamp.WordWrap = true;
stamp.XIndent = 389 * 0.75;
stamp.YIndent = 23 * 0.75;
page.AddStamp(stamp);
pdf.Save(dataDir + @"sample_1.pdf");

It seems like the pixels values get changed once image is added inside PDF document. Nevertheless, we have logged an investigation ticket as PDFNET-48423 in our issue tracking system. We will further check this in details and keep you posted with the status of ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi Asad,

Thanks again for getting back to me on this quickly.

Is there another way you can suggest apart from the way above? We want to have a searchable PDF from an image. We initially populate values in the image before we convert them to PDF however these values are not searchable. This is the reason we ended up on this approach.

I have tried Aspose.OCR as well but it only returns the text. Sometimes, depending on the quality of the image, it doesn’t give me the correct text output.

I am hopeful you could help me on this. We are also tight on time as we have to decide sooner whether we should proceed with an upgrade with you or look for another solution somewhere else.

Kind regards,
Ruby

@rfernando

You may please try following alternative approach in order to make searchable PDF:

public static string ConvertPDFToSearchable(string file)
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(file);
doc.Convert(CallBackGetHocr);
doc.Save(file + “-Version4.pdf”);
return file + “-Version4.pdf”;
}
static string CallBackGetHocr(System.Drawing.Image img)
{
		string dir = @"C:\Temp\";
		img.Save(dir + "ocrtest.jpg");
		ProcessStartInfo info = new ProcessStartInfo(@"C:\Program Files\Tesseract-OCR\tesseract.exe");
		info.WorkingDirectory = @"C:\Program Files\Tesseract-OCR";
		info.WindowStyle = ProcessWindowStyle.Hidden;
		info.Arguments = @"C:\Temp\ocrtest.jpg C:\Temp\out hocr";
		Process p = new Process();
		p.StartInfo = info;
		p.Start();
		p.WaitForExit();
		StreamReader streamReader = new StreamReader(@"C:\Temp\out.txt");
		string text = streamReader.ReadToEnd();
		streamReader.Close();
		return text;
	}

Thanks for the suggestion but I have looked at using Tesseract-OCR already and it didn’t give me the expected result even for the sample image I provided. We have much more complicated forms. The one I sent is one of the simplest forms we have. That’s also the reason we opted to go ahead on adding text to pdf instead.

As for the investigation ticket PDFNET-48423, how soon can we get a response on this?

@rfernando

The investigation ticket has recently been logged in our issue management system and is pending for analysis. It will be resolved on first come first serve basis. However, we have recorded your concerns and will surely take care of them during ticket investigation. Would you please provide us an output PDF document as well which you expect to generate using Aspose.PDF. This would help us investigating the ticket accordingly.