How to Remove Unwanted Line Breaks from PDF Comments via JavaScript
Question details
The user wants to remove unwanted carriage returns from text copied into PDF comments, while preserving line breaks that appear before notes or hyphenated text.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Formatting text passages copied from PDF highlights into comment contents, which often result in broken paragraphs and messy line breaks.
- Observed behavior
- The copied text retains hard line breaks at the end of every visual line, causing unnatural paragraph flows in the comment annotations.
Before running bulk JavaScript commands to modify annotations, save a copy of your PDF document, as executing batch scripts cannot always be undone with a simple undo command.
Run a Custom Script in the JavaScript Console
Use the built-in JavaScript console to loop through all annotations and automatically replace specific carriage returns with spaces.
Because PDFs store text based on physical placement rather than fluid paragraphs, copying highlighted text usually copies hard line returns. You can fix this by running a custom script that replaces carriage returns unless they are followed by a hyphen.
If you encounter a 'SyntaxError: syntax error 1:Console:Exec' message, it means the multi-line script was executed line-by-line rather than as a single block.
In Adobe Acrobat, open the JavaScript Console by pressing Ctrl+J on Windows or Cmd+J on Mac.
Paste the following code into the console window: this.syncAnnotScan(); var annots = this.getAnnots(); if (annots) { for (var i in annots) { var annot = annots[i]; annot.contents = annot.contents.replace(/\r([^-])/g, " $1"); } }
To avoid syntax errors, highlight the entire block of code you just pasted using your mouse. Then, press Ctrl+Enter (or the Enter key on your numeric keypad) to run the script.

Easily Manage PDF Comments with WPS Office
If you are tired of dealing with messy PDF formatting and complex JavaScript consoles, try using WPS Office. The built-in WPS PDF tool smartly handles text extraction, highlights, and annotations, preserving your paragraph flow without adding unnecessary line breaks.
- 1. Open your PDF: Launch WPS Office and open your PDF document using the built-in PDF viewer.
- 2. Use the Comment Tools: Navigate to the Comment tab to access highlights, sticky notes, and text commenting tools.
- 3. Extract Text Smoothly: Select the text you wish to highlight or copy. WPS PDF intelligently groups the selection into continuous paragraphs without inserting artificial line breaks.

Frequently Asked Questions
Why does copied text from PDFs have unwanted line breaks?
PDFs are designed to preserve visual page layouts, meaning text is positioned strictly on the page by coordinates. Unlike word processors, PDFs often treat the end of a visual line as a hard return instead of a continuous wrapping line, causing weird formatting when you copy and paste.
How do I avoid syntax errors when using the PDF JavaScript console?
When pasting multi-line code into the console, pressing the standard 'Enter' key executes the current line immediately, which breaks loops and if-statements. To prevent this, select the entire block of code with your cursor before pressing 'Ctrl + Enter' or 'Command + Return'.
Can I remove line breaks from text without using JavaScript?
Yes. You can paste the copied text into a word processor like WPS Writer and use the Find and Replace feature. Search for paragraph marks (^p) and replace them with a single space to clean up the formatting manually before pasting it back.




