After Creating link in a document, it is shifting

Hi,

I am trying to create a gotor link in a document(page35.pdf) with Aspose.PDF as below code snippet. After saving, the link is shifting every time on opening the document.

Sample Code:
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Aspose.Pdf.lic”);
Aspose.Pdf.Document targetDoc = null;
targetDoc = new Aspose.Pdf.Document($"{PartialPath} + “page35.pdf”);
page = targetDoc.Pages[1];
double llx = double.Parse(46);

double lly = page.MediaBox.Height - double.Parse(-154) - double.Parse(48) - 2;

double urx = llx + double.Parse(415) + 2;

double ury = lly + double.Parse(48) + 2;

AnnotRect = new Aspose.Pdf.Rectangle(llx, lly, urx, ury);

LinkAnnotation link = new LinkAnnotation(page, AnnotRect);
XYZExplicitDestination mag = new XYZExplicitDestination(page, -32768, -32768, 1);
link.Action = new GoToRemoteAction($"{PartialPath}_link.pdf", mag);

(link.Action as GoToRemoteAction).NewWindow = Aspose.Pdf.ExtendedBoolean.True;

page.Annotations.Add(link);
targetDoc.Save($"{PartialPath} + “page35.pdf”);

I have tried to create a link in adobe for the same document(page35.pdf) and it is not shifting there. Kindly let us know why it is shifting every time and help us to resolve the same.

page35.pdf (864.5 KB)

@prnksheela

Are you saying that you were not able to add the link using Adobe Reader too? Can you please explain a bit more? Are you trying to add a link to remote to open a new PDF? Can you please share expected output for our reference so that we can test the scenario in our environment and address it accordingly. We noticed that you are using some coordinates. From where and how you are getting them? Do you have some desired location in the PDF where you want to add the link?

I am able to add link through Adobe Reader without any issues.

Here I am trying to create GotoR link in this document.

We are selecting some area or text similar to Adobe reader in our application and then getting the coordinates.

You can try random coordinates as well, It is shifting irrespective of coordinates for this specific document.

We will surely try with the random coordinates. However, could you please share the output generated using Adobe Reader at your side? Also, please share the screenshot of the coordinates Adobe Reader showed you for this PDF. This will help us in investigating the case accordingly and apply fixes.

I have created a gotor link using Adobe Reader with coordinates as below, Attaching the document for your reference. page35 (1).pdf (855.1 KB)

double llx = double.Parse(71.5197);
double lly = page.MediaBox.Height - double.Parse(111.231) - double.Parse(55.136) - 2;
double urx = llx + double.Parse(111.231) + 2;
double ury = lly + double.Parse(55.136) + 2;

I suspect that the Media box value for this specific document causing issue with Y axis. Please check.

@prnksheela

Please check the below code snippet and the attached output PDF for your kind reference. We tested using 23.8 version of the API and we did not notice any issue in the output PDF.

Document pdfDocument = new Document(dataDir + "page35.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(@"SOURCE\s+DOCUMENTS\s+AND\s+CASE\s+REPORT\s+FORM\s+COMPLETION", new TextSearchOptions(true));
pdfDocument.Pages.Accept(textFragmentAbsorber);
foreach (var textFragment in textFragmentAbsorber.TextFragments)
{
    LinkAnnotation link = new LinkAnnotation(textFragment.Page, textFragment.Rectangle);
    XYZExplicitDestination mag = new XYZExplicitDestination(textFragment.Page, -32768, -32768, 1);
    link.Action = new GoToRemoteAction($"page35.pdf", mag);

    (link.Action as GoToRemoteAction).NewWindow = Aspose.Pdf.ExtendedBoolean.True;

    textFragment.Page.Annotations.Add(link);
}
pdfDocument.Save(dataDir + "output.pdf");

output.pdf (864.8 KB)

Can you please check the attached document and let us know if you find any issues in it?

For calculating rectangle for link, we are using below code,

double llx = double.Parse(46);

double lly = page.MediaBox.Height - double.Parse(-154) - double.Parse(48) - 2;

double urx = llx + double.Parse(415) + 2;

double ury = lly + double.Parse(48) + 2;

AnnotRect = new Aspose.Pdf.Rectangle(llx, lly, urx, ury);

Which is common for all document. But this document only behaving like this.

Kindly try with this formula which is mandatory in our code.

For Example, Please see for Y axis, when we pass like this it is shifting. Kindly check and let us know.

@prnksheela

Sure. Can you please also share the other PDF file with which this formula is generating correct and expected output? This will help in our investigation.

Please find a sample doc, where the links are created and not shifting on opening every timeRI.pdf (26.2 KB)

@prnksheela

Please check the below code snippet that we used with both of your files and Aspose.PDF for .NET 23.8.

Aspose.Pdf.Document targetDoc = null;
targetDoc = new Aspose.Pdf.Document($"{dataDir}RI.pdf");
var page = targetDoc.Pages[1];
double llx = 46;

double lly = page.MediaBox.Height - (-154) - (48) - 2;

double urx = llx + (415) + 2;

double ury = lly + (48) + 2;

Aspose.Pdf.Rectangle AnnotRect = new Aspose.Pdf.Rectangle(llx, lly, urx, ury);

LinkAnnotation link = new LinkAnnotation(page, AnnotRect);
XYZExplicitDestination mag = new XYZExplicitDestination(page, -32768, -32768, 1);
link.Action = new GoToRemoteAction($"{dataDir}_link.pdf", mag);

(link.Action as GoToRemoteAction).NewWindow = Aspose.Pdf.ExtendedBoolean.True;

page.Annotations.Add(link);
targetDoc.Save($"{dataDir}RI.out.pdf");

RI.out.pdf (24.2 KB)
page35.out.pdf (864.8 KB)

We have also attached the generated outputs for your kind reference. We could not find any link in both output PDFs while checking them in the Adobe Reader. Can you please confirm or share some screenshot for our better understanding?

image-2023-01-18-16-22-16-848.jpg (241.7 KB)

image-2023-01-18-16-21-48-802.jpg (351.1 KB)

Please see the screenshot for added links which shifting.

@prnksheela

Looks like you are using an older version of the API. Can you please run your same code snippet with Aspose.PDF for .NET 23.8 and share your feedback with us what issues do you notice? Please note that we only provide support on the basis of the latest version available at the moment. So, please try to use it with updated coordinates OR please share how did you calculate these coordinates? We will further proceed accordingly. We are sorry for the trouble.

I have tried with Aspose.PDF for .NET 23.9 version I am facing the same issue.

The coordinates provided is an example. In this document, whatever the coordinates the link is getting shifted after saving the document with below code for calculating the rectangle,

double llx = llx;

double lly = page.MediaBox.Height - (lly) - (height) - borderWidth;

double urx = llx + (width) + borderWidth;

double ury = lly + (height) + borderWidth;

Here right side of equation variables which was the co-ordinates derived from the runtime selection of a rectangle in PDF area similar to Acrobat.

Can you please share the values that you collected on runtime along with a screenshot of the rectangle with these values on PDF?

Rectangle coordinates and Screenshots.docx (471.8 KB)

I have provided selected coordinates through run time selection, please calculate rectangle coordinates using below formula,

double llx = llx;
double lly = page.MediaBox.Height - (lly) - (height) - borderWidth;
double urx = llx + (width) + borderWidth;
double ury = lly + (height) + borderWidth;

Please see that after create and saving the links, it is getting shifted. Kindly check and suggest.

@prnksheela

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-55534

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Is there any update?

@prnksheela

We are afraid that no updates are available at the moment about the ticket resolution. However, we will surely investigate and analyze the issue on a first come first serve basis and as soon as it is resolved, we will update you in this forum thread. Please be patient and spare us some time.

We are sorry for the inconvenience.