#1 2016-04-21 08:17:08

TRichView
Member
From: Tkachenko
Registered: 2016-04-21
Posts: 6
Website

CreateLink coordinates

I am trying to implement PDF export for our TRichView editor using SynPDF.
Everything is ok, except for links.

I do not understand which coordinates should I pass to CreateLink.
I assumed that pixels, but I am obviously wrong.

For example, for a test, I am trying to put a hyperlink around the whole page inside margins ((76, 76, 718, 1047) at 96 dpi).
The result is completely out of place and has wrong size:
synpdflink.png

Can you help me?

Offline

#2 2016-04-21 09:00:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: CreateLink coordinates

The PDF coordinates is not the same at all as Windows GDI coordinates.
It uses a 75 dpi resolution for the 1.0 value, but floating-point coordinates.
As such, there is no "pixel" in PDF.

There are several method helpers in SynPDF to change the pixel-like integer coordinates into proper PDF double coordinates.

How do you generate the pdf page? Using TMetaFile content?

Offline

#3 2016-04-21 09:42:02

TRichView
Member
From: Tkachenko
Registered: 2016-04-21
Posts: 6
Website

Re: CreateLink coordinates

I assign FPdfDoc.DefaultPaperSize (FPdfDoc is TPdfDocumentGDI)
Additionally, I assign FPdfDoc.ScreenLogPixels (currently to Screen.PixelsPerInch, but in future it may be another value).
Then call FPdfDoc.AddPage.
Then I draw onto FPdfDoc.VCLCanvas using pixel coordinates.

I have pixels positions of bookmarks and links (relative to the top of the page), how to convert them to coordinates for CreateLink and CreateBookmark?
I tried Value * 75 / 96, the results are close, but not exactly correct:

synpdflink2.png

Last edited by TRichView (2016-04-21 10:00:21)

Offline

#4 2016-04-21 16:12:27

TRichView
Member
From: Tkachenko
Registered: 2016-04-21
Posts: 6
Website

Re: CreateLink coordinates

Well, it should be 72, not 75.
With 72 dpi, scale and X coordinates are correct, however, the rectangle is shifted by Y, and may be shifted by a huge distance.

Offline

#5 2016-04-28 08:02:27

TRichView
Member
From: Tkachenko
Registered: 2016-04-21
Posts: 6
Website

Re: CreateLink coordinates

Well, probably I need to provide mode information.
Here is the example, I want to make a hyperlink around "Prep" text.
Content is made by drawing on VCLCanvas. The origin of coordinates is moved to the point marked with a green circle (using SetWindowOrgEx).

In the code below, I draw a red rectangle around the link area, and create the link.
Input:
  R - link rectangle, in pixels, relative to the origin.
  DocRect - rectangle of document inside the margin

      
     // drawing a red rectangle
      FPdfDoc.VCLCanvas.Brush.Style := bsClear;
      FPdfDoc.VCLCanvas.Pen.Width := 1;
      FPdfDoc.VCLCanvas.Pen.Style := psSolid;
      FPdfDoc.VCLCanvas.Pen.Color := clRed;
      FPdfDoc.VCLCanvas.Rectangle(R.Left, R.Top, R.Right, R.Bottom);
      // adding a link
      OffsetRect(R, DocRect.Left, DocRect.Top);
      dpi := Screen.PixelsPerInch;
      PdfRect.Left := R.Left * 72 / dpi;
      PdfRect.Top := R.Top * 72 / dpi;
      PdfRect.Right := R.Right * 72 / dpi;
      PdfRect.Bottom := R.Bottom * 72 / dpi;
      FPdfDoc.CreateLink(PdfRect, StringToUTF8(Copy(Target, 2, MaxInt)));

The result is below:
synpdflink3.png
Everything is correct except for Y coordinates. You can see that the link is shifted down a lot.

Last edited by TRichView (2016-04-28 08:04:01)

Offline

#6 2016-04-28 08:26:32

jonjbar
Member
Registered: 2012-12-27
Posts: 23

Re: CreateLink coordinates

From a quick overview of your screenshot, it looks like the Y is simply inverted for the link.
Perhaps the CreateLink's reference point is the bottom left of the document while the rectangle's reference is the top left ? Not sure why.

Offline

#7 2016-04-28 10:59:11

TRichView
Member
From: Tkachenko
Registered: 2016-04-21
Posts: 6
Website

Re: CreateLink coordinates

You are right, thank you!
Y positions for CreateBookmark and CreateLink are counted from the page bottom

Offline

#8 2016-04-28 11:17:09

TRichView
Member
From: Tkachenko
Registered: 2016-04-21
Posts: 6
Website

Re: CreateLink coordinates

While the issue is solved, I have some suggestions for future versions:
- to add explanations that sizes are measured in points, and coordinates are measured from the bottom left corner
- an option to suppress a black border for links
- ability to add links to external URLs and files; yes, URLs are auto-detected, but a visible text and a link target are not necessary identical.

By the way, I created a demo for making PDF files from RTF and RVF files using Synopse PDF Engine and our components:
http://www.trichview.com/forums/viewtopic.php?p=30151

Last edited by TRichView (2016-04-28 18:34:32)

Offline

#9 2016-05-04 08:17:56

jonjbar
Member
Registered: 2012-12-27
Posts: 23

Re: CreateLink coordinates

Great!
Perhaps you should create tickets: http://synopse.info/fossil/reportlist
Those would be great additions indeed.

Offline

Board footer

Powered by FluxBB