I want to be able to copy text from converted PDF documents into a Google Docs. What I am finding with the current document is that the text only covers the left side of the page. I want it to look like a normal Google Docs.

Obviously I have the option of going to the front of each line of text and pressing the delete key until it connects with the end of the previous line of text. However, this is obviously extremely time-consuming. I had hoped that by pasting without formatting it would remove this issue, but this has not been the case.

Can anyone advise me how to do what I want to do without using the delete button for every line individually as described?

1 Answer 1


Here is a one-line function that removes the leading whitespace at the beginning of every line of a document. Go to Tools > Script Editor, put the function there, save, and run.

function removeLeadingSpaces() {
  DocumentApp.getActiveDocument().getBody().editAsText().replaceText("(?m)^\\s+", "");

Here (?m) is multi-line flag, which makes ^ match the beginning of every line. And \s+ is "any amount of whitespace".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.