[Tex/LaTex] Fitting codes in a Latex Code block

codelistings

    \usepackage{listings}           
    \begin{document}

\lstset{language=Java}        

\begin{lstlisting}[frame=single]  

             PDDocument doc = PDDocument.load(file);
             String content = new PDFTextStripper().getText(doc);
             doc.close();
             return new Indexitem((long)file.getName().hashCode(), file.getName(), content);

\end{lstlisting}
    \end{document}

Considering the above code, my question is how to fit the code into its block? Assuming we are using a double-column template.

Best Answer

,breakatwhitespace,gobble=10 is an alternative solution:

\documentclass{article}
\usepackage{listings}           
\begin{document}

\lstset{language=Java}        
%\begin{lstlisting}[frame=single,gobble=14,basicstyle=\scriptsize\ttfamily]
%\begin{lstlisting}[frame=single,breaklines,breakatwhitespace,gobble=10]
\begin{lstlisting}[frame=single,breaklines]

             PDDocument doc = PDDocument.load(file);
             String content = new PDFTextStripper().getText(doc);
             doc.close();
             return new Indexitem((long)file.getName().hashCode(), file.getName(), content);

\end{lstlisting}
\end{document}