<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Deepak Singh</title>
    <description>The latest articles on DEV Community by Deepak Singh (@deepak_singh_5c5c8288cb10).</description>
    <link>https://dev.to/deepak_singh_5c5c8288cb10</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4266736%2Fd900bc71-7a96-445d-87f6-ff3456c35a38.jpg</url>
      <title>DEV Community: Deepak Singh</title>
      <link>https://dev.to/deepak_singh_5c5c8288cb10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deepak_singh_5c5c8288cb10"/>
    <language>en</language>
    <item>
      <title>Editing existing text in a PDF without losing the font</title>
      <dc:creator>Deepak Singh</dc:creator>
      <pubDate>Thu, 26 Nov 2026 11:17:31 +0000</pubDate>
      <link>https://dev.to/deepak_singh_5c5c8288cb10/editing-existing-text-in-a-pdf-without-losing-the-font-cl4</link>
      <guid>https://dev.to/deepak_singh_5c5c8288cb10/editing-existing-text-in-a-pdf-without-losing-the-font-cl4</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build PDFPro, a free online PDF editor. This post explains how its text editing works, including the parts that still do not.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The most common complaint about PDF editors: you change one word and it comes out in a different font. The easy way to build "editing" is a white box over the old text and new text on top in a default font. It works, but it rarely matches.&lt;/p&gt;

&lt;p&gt;I wanted edits that change the text itself and keep the original font. That turned out to be mostly a font problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  A PDF page has no "text", only drawing instructions
&lt;/h2&gt;

&lt;p&gt;Inside a page's content stream, text is a list of operators. A short line might look roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight postscript"&gt;&lt;code&gt;&lt;span class="no"&gt;BT&lt;/span&gt;
  &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="no"&gt;F1&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="nc"&gt;Tf&lt;/span&gt;          &lt;span class="c1"&gt;% select font resource F1 at 11 pt&lt;/span&gt;
  &lt;span class="mi"&gt;72&lt;/span&gt; &lt;span class="mi"&gt;700&lt;/span&gt; &lt;span class="nc"&gt;Td&lt;/span&gt;          &lt;span class="c1"&gt;% move to a position&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;0024004F0050&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Tj&lt;/span&gt;  &lt;span class="c1"&gt;% show these character codes&lt;/span&gt;
&lt;span class="no"&gt;ET&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things matter here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Tj&lt;/code&gt; (and its siblings &lt;code&gt;TJ&lt;/code&gt;, &lt;code&gt;'&lt;/code&gt; and &lt;code&gt;"&lt;/code&gt;) shows &lt;strong&gt;character codes&lt;/strong&gt;, not Unicode. What those codes mean depends on the font's encoding.&lt;/li&gt;
&lt;li&gt;To get readable text back, you go through the font's &lt;code&gt;ToUnicode&lt;/code&gt; map, if it has one.&lt;/li&gt;
&lt;li&gt;There are no words, lines or paragraphs. A generator may write a whole line in one operator, or one word per operator, or one glyph per operator.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So "edit this word" really means: find the operators that paint it, remove them, and draw new text in the same place with a font that can show it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: finding the right operator
&lt;/h2&gt;

&lt;p&gt;In the browser, the editor renders pages with pdf.js and reads text runs (string, position, width, height) with &lt;code&gt;getTextContent()&lt;/code&gt;. On &lt;strong&gt;Apply changes&lt;/strong&gt;, the client sends a list of operations to a Java backend that rewrites the PDF with Apache PDFBox 3.&lt;/p&gt;

&lt;p&gt;The catch: a pdf.js run is not a content-stream operator. One run can cover several operators, and blank runs are skipped, so a run index does not line up with an operator index.&lt;/p&gt;

&lt;p&gt;So the server uses geometry first. The client sends the box each run occupied, in page points. The server walks the content stream, tracks the text matrix, and empties every show operator whose starting point falls inside that box, within a baseline tolerance of 40% of the run height. If text is inside a form XObject, that form is cloned and processed the same way, up to four levels deep. Only when the box finds nothing does it fall back to matching by text: decode each operator to Unicode (the &lt;code&gt;ToUnicode&lt;/code&gt; map, or the font's encoding when there is none) and pick the matching operator nearest the expected index.&lt;/p&gt;

&lt;p&gt;"Empties" is literal: the operator stays in the stream and its string becomes empty. The original glyphs are gone from the file, not hidden under a box. While walking the stream, the server also tracks the fill colour through &lt;code&gt;q&lt;/code&gt;/&lt;code&gt;Q&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt;/&lt;code&gt;rg&lt;/code&gt;/&lt;code&gt;k&lt;/code&gt;/&lt;code&gt;sc&lt;/code&gt;/&lt;code&gt;scn&lt;/code&gt;, so for gray, RGB and CMYK text the new text keeps the old colour unless you choose a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: reusing the font, which is the hard part
&lt;/h2&gt;

&lt;p&gt;Many PDFs embed a &lt;strong&gt;subset&lt;/strong&gt; of each font: only the glyphs the document actually uses. The spec marks this with a six-uppercase-letter tag and a plus sign in the font name, for example &lt;code&gt;ABCDEF+Arial-BoldMT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Subsets cause two problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You cannot encode new text the normal way.&lt;/strong&gt; When a subset is made, glyph IDs can be renumbered, so asking the embedded font "which code is the letter a?" can give the wrong answer. Instead, the server builds its own table from the PDF's own data: for each code, look up its Unicode value, and, for TrueType-based CID fonts, keep it only if the embedded font program has a real outline for that glyph. Then map Unicode to the original code. New text is written as raw codes in a &lt;code&gt;Tj&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Simplified from the real code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;twoByte&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mh"&gt;0x10000&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;0x100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usedOnPage&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;usedOnPage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUnicode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;codePointCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;codePointAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// hasOutline only really checks TrueType CID fonts; others pass&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kt"&gt;Character&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isWhitespace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasOutline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putIfAbsent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;bytesOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;twoByte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;usedOnPage&lt;/code&gt; filter is there because some producers write a &lt;code&gt;ToUnicode&lt;/code&gt; map that lists every code as itself, even though the subset really uses glyph IDs. Those fake entries come first and would win, so when possible only codes still painted on the same page with that font are trusted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The letter you typed may not exist.&lt;/strong&gt; If the document never used a capital "Q", the subset has no "Q" glyph. No encoding trick fixes that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: what happens when a glyph is missing
&lt;/h2&gt;

&lt;p&gt;The check happens in the browser first. pdf.js registers embedded fonts as web font faces, so the editor can test a character by drawing it twice on a small canvas, once in the embedded face and once in the fallback, and comparing the pixels. If both drawings are identical, the font has no glyph for that character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;missing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nf"&gt;snapshot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sb"&gt;`30px "${family}", system`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;snapshot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'30px system'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spaces never count, results are cached, and fonts that are only named (not embedded) are skipped, because there is no face to compare against.&lt;/p&gt;

&lt;p&gt;When a character is missing, the editor asks you to choose a font for the edit. It preselects a suggestion based on the original font name: serif names map to Times, monospace names to Courier, common sans names to Helvetica, otherwise Noto Sans. On the server, Times, Courier and Helvetica are set with bundled Liberation Serif, Mono and Sans, after first looking for a font of that name already on the page. You can also pick a font from your own machine (Local Font Access API, where supported) or upload a font file.&lt;/p&gt;

&lt;p&gt;On the server, candidates are tried in a fixed order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. a font file the user picked
2. the original font (through the code table above)
3. the same family in the same weight and slant, if the page has it
4. a bundled Liberation Sans / Serif / Mono, chosen by family
5. bundled Noto Sans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first one that can show every character wins. If none can, the one that covers the most characters is used, and only the rest are replaced by "?" (or a blank, if even "?" is missing). A Standard 14 font is the last resort, used only when no candidate covers anything. If you picked a font or changed bold or italic, step 2 is skipped.&lt;/p&gt;

&lt;p&gt;One more case: if you choose to keep the original font in that prompt, and the subset can show at least half of the new text, the server keeps the document's own glyphs and borrows only the missing ones from a fallback font. For digits, when the document's own "0" is available, it scales the borrowed glyph with the &lt;code&gt;Tz&lt;/code&gt; (horizontal scaling) operator to that width, so columns of numbers stay aligned.&lt;/p&gt;

&lt;p&gt;Non-subset fonts already in the file have their own trap: the PDF often lists widths only for the characters the document used. Any other character falls back to the descriptor's &lt;code&gt;MissingWidth&lt;/code&gt;, usually 0, so every glyph would land on one spot. The resolver treats such characters as "cannot show".&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: keeping the layout
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Baseline and size.&lt;/strong&gt; New text starts at the exact baseline and x position of the original run, and the run's height from pdf.js is used as the font size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paragraphs.&lt;/strong&gt; Clicking text edits a single run. A separate paragraph tool groups lines into a paragraph when they have the same size, their left edges are close, and the line gaps are consistent:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sameSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nearLeft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// allows first-line indent&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;goodGap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that tool, the paragraph is edited as one block. When you apply changes, every original line is removed and the text is re-wrapped greedily, word by word, to the original width, using the measured line spacing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Text that grows.&lt;/strong&gt; If an edit needs more lines, the page opens a gap under it (on by default; a toolbar option turns it off). Everything below a cut line moves down and anything that crosses the cut (a table border, a cell background) stretches. This works on paths, text positions, images, annotation rectangles and the content of form XObjects, so a table row grows without the code detecting the table.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scanned PDFs need a different approach
&lt;/h2&gt;

&lt;p&gt;A scanned page is a picture: no text operators to empty, no font to reuse, nothing for pdf.js to extract.&lt;/p&gt;

&lt;p&gt;For those pages there is a separate &lt;strong&gt;Recognize text (beta)&lt;/strong&gt; feature. A Python OCR sidecar (PaddleOCR models through RapidOCR and ONNX Runtime, on CPU) finds the words and their letter boxes. To change a word, it paints out the old ink with the surrounding paper and builds the new word from letters cut out of the same page, so ink, blur and noise match. Letters that appear nowhere on the page are drawn with a fallback font, blurred to match. The edited image replaces the original in the PDF. It works on upright pages only.&lt;/p&gt;

&lt;h2&gt;
  
  
  What still does not work well
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Alignment.&lt;/strong&gt; Re-set lines start at the original left edge, so centred, right-aligned and justified paragraphs come out left-aligned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paragraph detection is a heuristic.&lt;/strong&gt; Multi-column layouts, hanging indents or mixed sizes can split a paragraph or merge two.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A known bug in the code table.&lt;/strong&gt; The server reads "codes used on the page" after the old run has been removed. A character that appeared only in the edited run can therefore be treated as missing and borrowed from a fallback font, even though the subset has it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replacement glyphs are close, not identical.&lt;/strong&gt; A glyph borrowed from Liberation or Noto will not match a custom brand font exactly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Colour.&lt;/strong&gt; Spot colours, patterns and other colour spaces are not tracked yet, so text in those can come out in the wrong colour.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Opening gaps has limits.&lt;/strong&gt; Rotated or skewed content, shadings and inline images are not moved, and a form that is drawn more than once follows only its first placement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scans are beta.&lt;/strong&gt; Results depend on scan quality, and the sidecar itself rates each edit as "good" or "rough".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where things run
&lt;/h2&gt;

&lt;p&gt;Rendering, text extraction, paragraph grouping and the glyph test happen in the browser. The PDF is uploaded (up to 50 MB) and rewritten on the server, where PDFBox runs. Uploaded and edited files are deleted automatically about 2 hours after upload, or right away with Delete files. No account is needed; without one, usage is limited to 50 actions per hour per IP address. All of the above is what &lt;a href="https://trypdfpro.com/pdf-editor" rel="noopener noreferrer"&gt;PDFPro's editor&lt;/a&gt; does when you edit existing text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;p&gt;If you build something similar: do not trust a subset's own cmap for encoding, do not assume one text run equals one operator, ask the user before swapping fonts, and treat scans as images, not text.&lt;/p&gt;

&lt;p&gt;Questions about PDF internals are welcome in the comments.&lt;/p&gt;

&lt;p&gt;I'm the developer of PDFPro (&lt;a href="https://trypdfpro.com/" rel="noopener noreferrer"&gt;https://trypdfpro.com/&lt;/a&gt;), so take my view of the trade-offs with that in mind.&lt;/p&gt;

</description>
      <category>pdf</category>
      <category>java</category>
      <category>javascript</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
