Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a custom text pointing to an URL #29

Closed
ThomasTJdev opened this issue Jul 11, 2020 · 14 comments
Closed

How to make a custom text pointing to an URL #29

ThomasTJdev opened this issue Jul 11, 2020 · 14 comments

Comments

@ThomasTJdev
Copy link
Contributor

Is it possible to create links to a website with a custom text? For example "My Google" which directs to www.google.com? I can only find doc.linkAnnot for hyperlinks, but it only takes Destination, where an URL doesn't fit well.

Thank you.

@ThomasTJdev
Copy link
Contributor Author

Oh well, just like this..

doc.setFillColor(initRGB("white"))
doc.drawText(45, y, "https://google.com")
doc.setFillColor(initRGB("black"))
doc.drawText(45, y, "LINK")

@ThomasTJdev
Copy link
Contributor Author

ThomasTJdev commented Jul 16, 2020

The solution above works fine, if the user opens the PDF in the Chrome. But if the users opens the PDF in Adobe, on mobile, etc., the link is not recognized and therefor not working.

Is there another way to create a hyperlink to an external website?

@ThomasTJdev ThomasTJdev reopened this Jul 16, 2020
@jangko
Copy link
Owner

jangko commented Jul 17, 2020

please take a look into demo/button.nim, I've just added example for creating hyperlink using PDF widget.

@ThomasTJdev
Copy link
Contributor Author

please take a look into demo/button.nim, I've just added example for creating hyperlink using PDF widget.

Works perfekt! Thank you very much 😃

@ThomasTJdev
Copy link
Contributor Author

Hi @jangko

The links works out of the box, if I view them in Chrome or Okular, but when viewing them on my mobile devices or Firefox, they don't work.
On mobile PDF readers the link is just not found, but in Firefox an error message tells me, that the link is formatted as a form:
This pdf contains forms. The filling of form fields is not supported.

I have been trying to adjust the code in widgets.nim, but until now no luck. My links are formatted as:

7 0 obj
<</Rect[127.5591 728.5039 290.4605 714.3307]/FT/Btn/Parent 69 0 R/Subtype/Widget/F 4/P 3 0 R/H/N/T(mInSwhqQHpjGENO)/AA<</D<</S/URI/IsMap true/URI(https:\057\057example.com)>>>>/DR 57 0 R/Type/Annot/DA(\057F3\04010.0000\040Tf\0400.0000\0400.0000\0400.0000\040rg)/Ff 65536/AP<</N 70 0 R>>/MK<</R 0/TP 0/CA()>>>>
endobj

whereas when I look into other PDF files formatting (with links), it looks like:

<</Type /Action/S /URI/URI (https://example.com)>> # within a rect or something

Do you know whats wrong, or in which direction I need to look to find a solution?

@jangko jangko reopened this Jul 25, 2020
@jangko
Copy link
Owner

jangko commented Jul 25, 2020

hmm, looks like the hyperlink is made through annotation action and not via widget action.

You can take a look in the PDF reference for detailed information about PDF internals.

Could you please send a link to your pdf example if allowed, perhaps using gist or something.
Then I can analyze it and make a fix to nimPDF annotation.

PDF spec is very dense, not only you and me have difficulty with it. foxit reader also have repeated regression. others PDF reader/writer may also have same problems.

@ThomasTJdev
Copy link
Contributor Author

I have inserted a PDF file here: 2020-07-25 - Testing NimPDF.pdf

Thanks for looking into it! And I'm happy to hear, that I'm not the only one have trouble with it 😉

@ThomasTJdev
Copy link
Contributor Author

Hi @jangko

Have you seen the attached PDF? I have looked at widgets.nim again, but I must realize, that I cant figure it out.

@ThomasTJdev
Copy link
Contributor Author

Hi @jangko

I have tried to look into it again but with no luck. If you are interested, I would like to sponsor/pay for this feature.

@jangko
Copy link
Owner

jangko commented Aug 29, 2020

I'm sorry I cannot accept your offer. Actually you can help speed up this process by finding a working example and send it to me, but the example should be working reliably at your target platform/reader. And then I can synthesize/imitate it for you.

@ThomasTJdev
Copy link
Contributor Author

Thank you @jangko.

I have attached a working example here: Test of hyperlink.pdf

The file has been generated with Word Office 365.

Tested on following devices:

Android:

  • Internal PDF viewer
  • Adobe Acrobat
  • Dropbox PDF-viewer
  • Google PDF-viewer
  • Word Office 365

iPhone:

  • PDF Expert
  • Adobe Acrobat

Linux:

  • Okular
  • ePDFviewer
  • Evince (Document Viewer)

Windows:

  • Bluebeam
  • Adobe Acrobat

Browser:

  • Firefox
  • Chromium
  • Safari
  • Google Drive PDF viewer

Document details

When I inspect the document, the structure is identical to other PDF files with working links:

<</Subtype/Link/Rect[ 54.45 621.93 94.485 644.42] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(http://www.google.dk/) >>/StructParent 1>>

Extract of document details: The link annotation is shown in the Userdata Results.

Expand document details
PDF Checker 1.5.0  Copyright 2018-2019 Datalogics, Inc. All Rights Reserved

Sun Aug 30 00:52:41 2020

JSON Profile: everything.json

Input Document: Testofhyperlink.pdf

File Size: 36.7 KB

<<=CHECKER_SUMMARY_START=>>
general:born-digital
userdata:contains-annots
userdata:contains-metadata
sizeInBytes:37617
canBeOptimized
<<=CHECKER_SUMMARY_END=>>

Optimization Assessment
    Document can be optimized with PDF Optimizer - see details below

General Results
    Errors:
        None
    Information:
        Document was born digital.  It was produced from PDF authoring software and so it may contain text, images, tables, forms, and other objects.  These types of PDFs typically do not require OCR.
    Checks Completed:
        born-digital
        claims-pdfa-conformance
        claims-pdfe-conformance
        claims-pdfua-conformance
        claims-pdfvt-conformance
        claims-pdfx-conformance
        contains-owner-password
        contains-signature
        damaged
        image-only
        password-protected
        pdf-v2
        unable-to-open
        xfa-type

Userdata Results
    Errors:
        None
    Information:
        Contains annotations: 
            SubType: Link (1 instance)
        Contains metadata: 
            SubType: XML, Update region size: 0 (1 instance)
    Checks Completed:
        contains-annots
        contains-annots-not-for-printing
        contains-annots-not-for-viewing
        contains-annots-without-normal-appearances
        contains-embedded-files
        contains-metadata
        contains-optional-content
        contains-private-data
        contains-transparency
    How To Optimize:
            Annotations can be removed using PDF Optimizer to save space. (1 instance)

Fonts Results
    Errors:
        None
    Information:
        None
    Checks Completed:
        fontdescriptor-missing-capheight
        fontdescriptor-missing-fields
        uses-base14fonts-not-embedded
        uses-fonts-fully-embedded
        uses-fonts-not-embedded

Objects Results
    Errors:
        None
    Information:
        None
    Checks Completed:
        contains-javascript-actions
        contains-thumbnails

Cleanup Results
    Errors:
        None
    Information:
        None
    Checks Completed:
        suboptimal-compression

Image Results
    Errors:
        None
    Information:
        None
    Checks Completed:
        alternate-images

    Color Images
    Errors:
        None
    Information:
        None
    Checks Completed:
        image-depth
        resolution-too-high
        resolution-too-low
        uses-jpeg2000-compression

    Grayscale Images
    Errors:
        None
    Information:
        None
    Checks Completed:
        resolution-too-high
        resolution-too-low
        uses-jpeg2000-compression

    Monochrome Images
    Errors:
        None
    Information:
        None
    Checks Completed:
        resolution-too-high
        resolution-too-low
        uses-jbig2-compression

@jangko
Copy link
Owner

jangko commented Aug 31, 2020

@ThomasTJdev, actually it is a simple URI annotation. But honestly, when I read the PDF reference, the meaning is not clear.
Luckily your example doesn't cluttered with unrelated stuff. I've added a new uriAnnot API, and you can see the example in demo/annot_uri.nim. I hope it works now. you can see PR #32 for implementation details.

I've only test it with Foxit Reader 9.7.1 and FireFox browser on Windows

@ThomasTJdev
Copy link
Contributor Author

I have now tested it in development and in production - and it works flawless on all devices 😃 ! Thank you very much @jangko!

@jangko
Copy link
Owner

jangko commented Sep 1, 2020

glad it works. 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants