0

I need to copy some sections of source code from a pdf-file to a java project using eclipse neon.

Let me give you a quick example of the problem. I have code which looks like the following in the pdf:

import java.sql.Timestamp; 
import twitter4j.FilterQuery;
import twitter4j.Status; 
import twitter4j.StatusAdapter;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener; 
import twitter4j.TwitterException; 
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory; 

public final class PrintSampleStream extends StatusAdapter { 

public static void main(String[] args) throws TwitterException{
...

But in the workspace it is shown like this:

import java.sql.Timestamp; import twitter4j.FilterQuery; import twitter4j.Status; import twitter4j.StatusAdapter; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener; import twitter4j.TwitterException; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; public final class PrintSampleStream extends StatusAdapter { public static void main(String[] args) throws TwitterException{

How can i format the code and make it readable? Because formatting the code by hand would take too long.

Thanks for any ideas

P.S. CTRL + Shift +F doesn't help

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
  • Are the comments commenting out parts of the code? i.e. `int i // variable i int j` "variable i" si supposed to be a comment but "int j" is supposed to be code. – MatheM Sep 01 '16 at 08:50
  • Well, your problem is that you're missing the newline. Otherwise the comments would not be an issue. What method are you using for copying the code? What operating system are you doing this on? If you're using manual copy-paste, what software are you using to display the PDF? – RealSkeptic Sep 01 '16 at 08:53
  • The issue of missing linebreaks after copying is called `Text Reflow Issue` try to google how to fix this for pdf reader you are using. Try using multiple different pdf readers to display the pdf file you are copying from. – MatheM Sep 01 '16 at 09:09
  • debian+adobe, just copy+paste – Jürgen K. Sep 01 '16 at 09:21
  • My suggestion would be to use like pdftotext which is pretty good at keeping formatting and then copy the relevant code out of the textfile. – Stefan Hegny Sep 01 '16 at 20:00

3 Answers3

1

It's a whitespace issue in the PDF or it's copy operation. You can replace string ; (semicolon+space) by ;+ newline using the Eclispe search/replace dialog. See In Eclipse, how do I replace a character by a new line?

Community
  • 1
  • 1
Mickael
  • 3,506
  • 1
  • 21
  • 33
  • Can't replace using alt+shift+r. "Operation unavailable on the current selection" – Jürgen K. Sep 01 '16 at 09:20
  • So your current code editor isn't a Java editor? Also, the regular Ctrl+F dialog should be able to do similar things. – Mickael Sep 01 '16 at 09:21
  • well yes it actually is. I'm using eclipse neon. ctrl+f worked well replacing. But even after reformatting the format didn't change – Jürgen K. Sep 01 '16 at 09:24
0

You say that ctrl+shift+F doesn't work for you. There is another way to format editor contents.

Go to: Window > Preferences > Java > Editor > Save actions and check the options Perform the selected actions on save, Format source code, Format all lines. Apply changes and then make small change in the editor (just to make it dirty) and save. Editor should automatically format.

Note: I am using Eclipse Luna but I believe that preference structure will be similar in Neon.

As for the reflow issues when copy pasting, you can check if this article helps you solve it.

MatheM
  • 801
  • 7
  • 17
  • I followed your instruction. Result->nothing changed. Get the following message while saving: "A save participant caused problems. The save participant 'Code Clean Up' caused an exception: java.lang.ArrayIndexOutOfBoundsException. See the error log for details." – Jürgen K. Sep 01 '16 at 13:23
  • @JürgenK. Then I am at a loss. Your eclipse is kaput. Does formatting work for you on other files? Or is it completely broken and no matter which file you open it won't work? – MatheM Sep 01 '16 at 15:08
  • The formatting works well on other files. I think it must be something about the "copying from pdf" – Jürgen K. Sep 05 '16 at 08:35
-1

This process isn't automated but you could use some free online code beautifiers such as

You basically have to copy-paste your unformatted code into a text-box. You can then "beautify" the code, by clicking a button. It then generates a formatted code. It isn't perfect, but it makes your code a lot more readable.

Example input:

import java.sql.Timestamp; import twitter4j.FilterQuery; import twitter4j.Status; import twitter4j.StatusAdapter; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener;public final class PrintSampleStream extends StatusAdapter { public static void main(String[] args) throws TwitterException{

Example output:

import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusAdapter;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
public final class PrintSampleStream extends StatusAdapter {
    public static void main(String[] args) throws TwitterException {