You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2022/08/28 08:35:26 UTC
svn commit: r1903723 - /pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Author: tilman
Date: Sun Aug 28 08:35:26 2022
New Revision: 1903723
URL: http://svn.apache.org/viewvc?rev=1903723&view=rev
Log:
PDFBOX-4892: remove unneeded null check and exception catch
Modified:
pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Modified: pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java?rev=1903723&r1=1903722&r2=1903723&view=diff
==============================================================================
--- pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java (original)
+++ pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java Sun Aug 28 08:35:26 2022
@@ -110,167 +110,154 @@ public class TextToPDF
*/
public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
{
- try
- {
-
- final int margin = 40;
- float height = font.getBoundingBox().getHeight() / FONTSCALE;
- PDRectangle actualMediaBox =
- landscape ? new PDRectangle(mediaBox.getHeight(), mediaBox.getWidth()) : mediaBox;
-
- //calculate font height and increase by a factor.
- height = height*fontSize*LINE_HEIGHT_FACTOR;
- BufferedReader data = new BufferedReader( text );
- String nextLine;
- PDPage page = new PDPage(actualMediaBox);
- PDPageContentStream contentStream = null;
- float y = -1;
- float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
+ final int margin = 40;
+ float height = font.getBoundingBox().getHeight() / FONTSCALE;
+ PDRectangle actualMediaBox =
+ landscape ? new PDRectangle(mediaBox.getHeight(), mediaBox.getWidth()) : mediaBox;
+
+ //calculate font height and increase by a factor.
+ height = height * fontSize * LINE_HEIGHT_FACTOR;
+ BufferedReader data = new BufferedReader(text);
+ String nextLine;
+ PDPage page = new PDPage(actualMediaBox);
+ PDPageContentStream contentStream = null;
+ float y = -1;
+ float maxStringLength = page.getMediaBox().getWidth() - 2 * margin;
- // There is a special case of creating a PDF document from an empty string.
- boolean textIsEmpty = true;
+ // There is a special case of creating a PDF document from an empty string.
+ boolean textIsEmpty = true;
- StringBuilder nextLineToDraw = new StringBuilder();
+ StringBuilder nextLineToDraw = new StringBuilder();
- while( (nextLine = data.readLine()) != null )
+ while ((nextLine = data.readLine()) != null)
+ {
+ // The input text is nonEmpty. New pages will be created and added
+ // to the PDF document as they are needed, depending on the length of
+ // the text.
+ textIsEmpty = false;
+
+ String[] lineWords = nextLine.replaceAll("[\\n\\r]+$", "").split(" ", -1);
+ int lineIndex = 0;
+ while (lineIndex < lineWords.length)
{
-
- // The input text is nonEmpty. New pages will be created and added
- // to the PDF document as they are needed, depending on the length of
- // the text.
- textIsEmpty = false;
-
- String[] lineWords = nextLine.replaceAll("[\\n\\r]+$", "").split(" ", -1);
- int lineIndex = 0;
- while( lineIndex < lineWords.length )
+ nextLineToDraw.setLength(0);
+ boolean addSpace = false;
+ float lengthIfUsingNextWord = 0;
+ boolean ff = false;
+ do
{
- nextLineToDraw.setLength(0);
- boolean addSpace = false;
- float lengthIfUsingNextWord = 0;
- boolean ff = false;
- do
- {
- String word1, word2 = "";
- String word = lineWords[lineIndex];
- int indexFF = word.indexOf('\f');
- if (indexFF == -1)
- {
- word1 = word;
- }
- else
- {
- ff = true;
- word1 = word.substring(0, indexFF);
- if (indexFF < word.length())
- {
- word2 = word.substring(indexFF + 1);
- }
- }
- // word1 is the part before ff, word2 after
- // both can be empty
- // word1 can also be empty without ff, if a line has many spaces
- if (word1.length() > 0 || !ff)
+ String word1, word2 = "";
+ String word = lineWords[lineIndex];
+ int indexFF = word.indexOf('\f');
+ if (indexFF == -1)
+ {
+ word1 = word;
+ }
+ else
+ {
+ ff = true;
+ word1 = word.substring(0, indexFF);
+ if (indexFF < word.length())
{
- if (addSpace)
- {
- nextLineToDraw.append(" ");
- }
- else
- {
- addSpace = true;
- }
- nextLineToDraw.append(word1);
+ word2 = word.substring(indexFF + 1);
}
- if (!ff || word2.length() == 0)
+ }
+ // word1 is the part before ff, word2 after
+ // both can be empty
+ // word1 can also be empty without ff, if a line has many spaces
+ if (word1.length() > 0 || !ff)
+ {
+ if (addSpace)
{
- lineIndex++;
+ nextLineToDraw.append(" ");
}
else
{
- lineWords[lineIndex] = word2;
- }
- if (ff)
- {
- break;
- }
- if( lineIndex < lineWords.length )
- {
- // need cut off at \f in next word to avoid IllegalArgumentException
- String nextWord = lineWords[lineIndex];
- indexFF = nextWord.indexOf('\f');
- if (indexFF != -1)
- {
- nextWord = nextWord.substring(0, indexFF);
- }
-
- String lineWithNextWord = nextLineToDraw + " " + nextWord;
- lengthIfUsingNextWord =
- (font.getStringWidth( lineWithNextWord )/FONTSCALE) * fontSize;
+ addSpace = true;
}
+ nextLineToDraw.append(word1);
}
- while (lineIndex < lineWords.length && lengthIfUsingNextWord < maxStringLength);
-
- if( y < margin )
+ if (!ff || word2.length() == 0)
{
- // We have crossed the end-of-page boundary and need to extend the
- // document by another page.
- page = new PDPage(actualMediaBox);
- doc.addPage( page );
- if( contentStream != null )
- {
- contentStream.endText();
- contentStream.close();
- }
- contentStream = new PDPageContentStream(doc, page);
- contentStream.setFont( font, fontSize );
- contentStream.beginText();
- y = page.getMediaBox().getHeight() - margin + height;
- contentStream.newLineAtOffset(margin, y);
+ lineIndex++;
}
-
- if( contentStream == null )
+ else
{
- throw new IOException( "Error:Expected non-null content stream." );
+ lineWords[lineIndex] = word2;
}
- contentStream.newLineAtOffset(0, -height);
- y -= height;
- contentStream.showText(nextLineToDraw.toString());
if (ff)
{
- page = new PDPage(actualMediaBox);
- doc.addPage(page);
+ break;
+ }
+ if (lineIndex < lineWords.length)
+ {
+ // need cut off at \f in next word to avoid IllegalArgumentException
+ String nextWord = lineWords[lineIndex];
+ indexFF = nextWord.indexOf('\f');
+ if (indexFF != -1)
+ {
+ nextWord = nextWord.substring(0, indexFF);
+ }
+
+ String lineWithNextWord = nextLineToDraw + " " + nextWord;
+ lengthIfUsingNextWord
+ = (font.getStringWidth(lineWithNextWord) / FONTSCALE) * fontSize;
+ }
+ }
+ while (lineIndex < lineWords.length && lengthIfUsingNextWord < maxStringLength);
+
+ if (y < margin)
+ {
+ // We have crossed the end-of-page boundary and need to extend the
+ // document by another page.
+ page = new PDPage(actualMediaBox);
+ doc.addPage(page);
+ if (contentStream != null)
+ {
contentStream.endText();
contentStream.close();
- contentStream = new PDPageContentStream(doc, page);
- contentStream.setFont(font, fontSize);
- contentStream.beginText();
- y = page.getMediaBox().getHeight() - margin + height;
- contentStream.newLineAtOffset(margin, y);
}
+ contentStream = new PDPageContentStream(doc, page);
+ contentStream.setFont(font, fontSize);
+ contentStream.beginText();
+ y = page.getMediaBox().getHeight() - margin + height;
+ contentStream.newLineAtOffset(margin, y);
}
- }
- // If the input text was the empty string, then the above while loop will have short-circuited
- // and we will not have added any PDPages to the document.
- // So in order to make the resultant PDF document readable by Adobe Reader etc, we'll add an empty page.
- if (textIsEmpty)
- {
- doc.addPage(page);
+ if (contentStream == null)
+ {
+ throw new IOException("Error:Expected non-null content stream.");
+ }
+ contentStream.newLineAtOffset(0, -height);
+ y -= height;
+ contentStream.showText(nextLineToDraw.toString());
+ if (ff)
+ {
+ page = new PDPage(actualMediaBox);
+ doc.addPage(page);
+ contentStream.endText();
+ contentStream.close();
+ contentStream = new PDPageContentStream(doc, page);
+ contentStream.setFont(font, fontSize);
+ contentStream.beginText();
+ y = page.getMediaBox().getHeight() - margin + height;
+ contentStream.newLineAtOffset(margin, y);
+ }
}
+ }
- if( contentStream != null )
- {
- contentStream.endText();
- contentStream.close();
- }
+ // If the input text was the empty string, then the above while loop will have short-circuited
+ // and we will not have added any PDPages to the document.
+ // So in order to make the resultant PDF document readable by Adobe Reader etc, we'll add an empty page.
+ if (textIsEmpty)
+ {
+ doc.addPage(page);
}
- catch( IOException io )
+
+ if (contentStream != null)
{
- if( doc != null )
- {
- doc.close();
- }
- throw io;
+ contentStream.endText();
+ contentStream.close();
}
}