Skip to content

Add a way to make text non-selectable #2249

Open

Description

@NicoWeio

Description

There are occasions where text is not meant to be selected or searched for, such as watermarks (see below).
I don't know how easy this is to implement with PDF, but it would be a helpful addition to Typst's already-great featureset.

Use Case

Typst's documentation provides a good example:

#set page(background: rotate(24deg,
  text(18pt, fill: rgb("FFCBC4"))[
    *CONFIDENTIAL*
  ]
))

= Typst's secret plans
In the year 2023, we plan to take
over the world (of typesetting).

Activity

  1. added
    feature request
    on Sep 26, 2023
  2. changed the title [-]Add option to make text non-selectable[/-] [+]Add a way to make text non-selectable[/+] on Sep 26, 2023
  3. LuxxxLucy commented on Sep 27, 2023

    @LuxxxLucy
    Contributor

    I think there are two ways for implementing this feature:

    One is to add a flag in text element to indicate that when are exporting to pdf, we do not write the text but rather transform the glyph into an outline shape and write to the pdf. This way the text will be non selectable. A possible interface could be #text(selectable : false, aaa)

    The second way is to utilize the ReplacementText feature (sec 14.9.4 PDF 1.7), it would be able to specify a different string when selected. For example, a possible interface would be #text(replacement: "bbb", aaa) and the result rendering would be aaa, but when select and copied out we will have bbb. The latex package called accsupp implemented this feature. In the second approach the non selectable text can be achieved by setting the replacement to be an empty string, such as #text(replacement: "", aaa)

    I personally think the second approach would be a more general design, its only drawback being that it requires the pdf reader to support the replacement feature (though I assume most of popular pdf reader should already support). What do you think?

  4. NicoWeio commented on Sep 27, 2023

    @NicoWeio
    Author

    Thank you for your detailed answer! I'd also prefer the second approach.

    For the future (from my understanding, this is not currently a feature), we'd also need a way to control whether text from a SVG should be selectable, so it would make sense to consider both aspects when making a design decision. There's an issue for this latter part already: #1239.

  5. added
    pdf
    text
    on Sep 28, 2023
  6. laurmaedje commented on Sep 28, 2023

    @laurmaedje
    Member

    The second approach or something similar to that is definitely preferrable. Outlining the glyphs will make it look different from other text because text rendering is somewhat platform-specific.

  7. LuxxxLucy commented on Oct 1, 2023

    @LuxxxLucy
    Contributor

    Hi I just tried out the text replacement approach. Good news is that it actually works (and the current pdf-writer we are using support it so we do not need PRs on pdf-writer), the bad news is that only seems to be working in acrobat reader, but not in chrome, apple preview, etc. The feature is not widely supported, which is not what I expected.

    This is a test example. aaabbb.pdf

    Essentially this is the normal pdf text block

    stream
    BT
    /F1 14 Tf
    108 734 Td
    (aaa) Tj
    ET
    endstream
    

    and this is the one with a ActualText block

    stream
    BT
    /F1 14 Tf
    108 734 Td
    /Span <<
      /ActualText (bbb)
    >> BDC
    (aaa) Tj
    EMC
    ET
    endstream
    

    If you open it with Adobe acrobat,

    • it renders aaa
    • if you copy it out it will be bbb
    • if you search aaa you find nothing
    • if you search with bbb, you will find it corresponding to aaa.

    However, I've tested with chrome and apple preview, where the feature is not supported (meaning you can still select the aaa and copied it out, and we can search aaa successfully but cannot find bbb).

    I also tried with the case of setting the replacement text to be a null empty string. It also works in acrobat but not in the other pdf readers .

    So the situation kind of makes me frustrated. I find this link about the accsupp latex package, which said that the accessibility features (including the ActualText we are talking) is not really supported by the many popular PDF readers.

    How should we proceed about this issue (supporting non selectable text)? Convert glyphs to shapes can do the job, but then that is not a good idea in terms of the rendering result; My idea is that that if converting to shapes is the only way, then perhaps we should not consider supporting it.

  8. laurmaedje commented on Oct 12, 2023

    @laurmaedje
    Member

    Getting garbage values is a lot easier than that actually. You just need to not write a /ToUnicode mapping, which is separate from the font. But as far as I understood, the desired feature was to make text non-selectable, not make it selectable but garbage when copy-pasted.

  9. laurmaedje commented on Oct 12, 2023

    @laurmaedje
    Member

    Without the /ToUnicode mapping, it will be garbage by default. Of course there are ways to reconstruct it, but if it is displayed, you can reconstruct it with OCR anyways.

  10. laurmaedje commented on Oct 12, 2023

    @laurmaedje
    Member

    It depends on how you write the PDF. The PDFs Typst writes would be garbage by default because we use character id to glyph id identity encoding. If you write ASCII with PDFDocEncoding then it will work.

    That aside, @reknih wrote on Discord:

    A smart reader may not select a water mark if correctly tagged as a watermark artifact (PDF 1.7, 17.8.2.2.2, also see note 2 in that section)

  11. reknih commented on Oct 12, 2023

    @reknih
    Member

    To comment on the other approaches:

    One is to add a flag in text element to indicate that when are exporting to pdf, we do not write the text but rather transform the glyph into an outline shape and write to the pdf. This way the text will be non selectable. A possible interface could be #text(selectable : false, aaa)

    This one would be easy to implement, work universally, result in sub-par text rendering, and be in flagrant violation of PDF/A and PDF/UA.

    The second way is to utilize the ReplacementText feature (sec 14.9.4 PDF 1.7), it would be able to specify a different string when selected. For example, a possible interface would be #text(replacement: "bbb", aaa) and the result rendering would be aaa, but when select and copied out we will have bbb. The latex package called accsupp implemented this feature. In the second approach the non selectable text can be achieved by setting the replacement to be an empty string, such as #text(replacement: "", aaa)

    Does not work in some readers. Violates 6.8.7 of PDF/A-1a.

    Fonts that do not correctly to Unicode are not a good idea. The only viable approaches are tagged PDF as an artifact (not universally supported) and outlining the glyphs (not accessible). I think we should have more specific elements for watermark etc. that just have the right UX in viewers. Also makes HTML export easier.

  12. Kreijstal commented on Nov 24, 2023

    @Kreijstal

    if you export to svg all text is already unselectable, so you can just convert to svg to pdf again, and boom unselectable text

  13. tv42 commented on Sep 6, 2024

    @tv42

    I have a habit of putting a whole-page sized rotated "draft" watermark on things, and having the watermark text selectable is annoying because for some reason mouse selection prefers the watermark over the body text. This would be very useful.

  14. SylvanFranklin commented on Nov 11, 2024

    @SylvanFranklin

    I also think this would be an awesome feature. I want to make it more annoying for people trying to copy my homework!

  15. Kreijstal commented on Nov 11, 2024

    @Kreijstal

    I also think this would be an awesome feature. I want to make it more annoying for people trying to copy my homework!

    do people still do this in the age of LLMs?
    also they can screenshot it and ask an LLM.

  16. SylvanFranklin commented on Nov 11, 2024

    @SylvanFranklin

    You make a good point, but I left out some info to make myself sound less petty. I'm in class that makes it purposefully difficult to copy answers into an external document, so I have to screenshot and OCR them. I wanted to give the graders a taste of there own medicine because they are lazy and use autograding software. There are obviously better justifications for this issue like the watermark example.

  17. Kreijstal commented on Nov 11, 2024

    @Kreijstal

    You make a good point, but I left out some info to make myself sound less petty. I'm in class that makes it purposefully difficult to copy answers into an external document, so I have to screenshot and OCR them. I wanted to give the graders a taste of there own medicine because they are lazy and use autograding software. There are obviously better justifications for this issue like the watermark example.

    based

  18. abitofathinker commented on Jan 16, 2025

    @abitofathinker

    I have a habit of putting a whole-page sized rotated "draft" watermark on things, and having the watermark text selectable is annoying because for some reason mouse selection prefers the watermark over the body text. This would be very useful.

    I wanted to bump this - my boss' feedback when I gave him a typst pdf report I'd made was that it was a pain to review because the "draft" watermark gets selection priority (and also because selecting text that included references selected the sidenote reference as well. Sidenotes are from the tufte-memo template). I confirmed this selection behavior in Acrobat 2024.05.20320.

    I love typst! It's awesome! Thanks!

  19. willismonroe commented on Jan 31, 2025

    @willismonroe

    I posted about this on the typst forum and was directed here.

    I'll give my use-case just for comparison. I'd like to be able to upload my lecture slides for my students but have LLMs struggle to easily ingest the text (I'll provide selectable versions for students with accomodations requiring such). For this use case either non-selectable, or garbage text would work fine.

    However, I just tested ChatGPT and NotebookLLM and both of them did not abide by the /ActualText specification mentioned above: #2249 (comment) so in this case non-selectable text would be preferred.

    In a pinch I can just use ImageMagick's convert to convert the PDF to PNG and then back to PDF.

  20. LuxxxLucy commented on Feb 23, 2025

    @LuxxxLucy
    Contributor

    Hey I just want to mention that, according from @reknih's comment, one way to achieve the non-selectable text is to use the Artifact tag, but I am a little confused when I dig into it. I tried some way (see a draft implementation in #5938 by introducing a watermark element) and it turns out the Artifact tag might not actually make it non-selectable. I tried with Adobe Acrobat and even it does not support it.

    Worried that #5938 might be wrong, I decide to make a small sample PDF first.
    Now I include a simple example here that is manually created.

    aaa.pdf

    where the essential part is

    
    5 0 obj
    <<
      /Length 75
    >>
    stream
    BT
    /F1 14 Tf
    /Artifact <</Type /Pagination >> BDC
    108 734 Td
    (aaa) Tj
    EMC
    ET
    endstream
    endobj
    

    PDF 1.7 14.8.2.2.2 does appear to indicate that the artifact tag used in this way can make text non selectable (or it is recommended to be implemented like this) but in my machine/setup it seems not working for all PDF readers I have.

    I wonder does anyone can confirm this using your machine or favouriate PDF readers? (I used free Adobe Acrobat) maybe the paid Adobe Acrobat will support this?

    My reasoning is that if the artifact tag appraoch is not working even for Arobat, perhaps we need find new ways to do it?

    A sudden idea that comes to my mind is that, can we make the true coordinate of text in one position, and the coordinate of text selection in another position, so that when the user tries to select in the place the text appears to be, the user would not successfully select anything. Does it sound like a good idea?

  21. kmlhpk commented on Feb 24, 2025

    @kmlhpk

    @LuxxxLucy Can confirm SumatraPDF v3.5.2 is still able to select the text

  22. laurmaedje commented on Sep 23, 2026

    @laurmaedje
    Member

    For posterity after the closing of #7295: The latest conclusion reached on how to realize this is documented in #7295 (comment).

  23. andylizi commented on Nov 1, 2026

    @andylizi
    Contributor

    Second attempt to add set text(selectable: false) in #8902.

    This PR only covers page-level text and does not support text in SVGs yet. I'm wondering: do we have a concrete use case for making SVG text unselectable1? It is a lot harder to support for architectural reasons.

    Footnotes

    1. It's always possible to achieve this by manually convert the SVG files. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestpdftext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions