DEV Community

KeyPDF
KeyPDF

Posted on

Why PDF text editing is so hard and how I approached it.

Why PDF Text Editing Is So Hard

If you have ever tried to edit PDF files you end up on websites that make an overlay or don't preserve the layout. PDF files are basically everywhere, from work to the educational field, and at first glance it seems like it should be easy to edit, just like a Word document. In reality it is not, and I realized that during the development of my own PDF editor keypdf, with the main feature to edit existing text.

A PDF format doesn't know what a paragraph is

Unlike a Word document that stores layout parameters, a PDF file only stores position, color, size and font, so it doesn't know how to group text but only how to draw it. Basically a PDF file is closer to an image with static text that looks the same on every device than to a Word document. With that knowledge, the idea of true textbox editing becomes much more challenging than just stamping new text on top and calling it editing, just like 99% of low quality PDF tools do, as there is simply no way to make it simple, since the algorithms for grouping and editing PDF content are mostly all proprietary, and the only open source solutions for JavaScript are mostly about annotations and page manipulations (rotation, cropping, merging).

What I did instead

I wanted real text editing, and therefore made my own grouping algorithm alongside a custom rendering and editing engine, without relying on 3rd party PDF tools, and as a result got a complete structure with code that communicates directly with every part of the editing and viewing, without building bridges between existing JavaScript libraries and gluing it into yet another PDF editor.

That is the whole thing I built KeyPDF around, and made it local without any limits.

Top comments (0)