I have read your comment to my previous answer:
I just want to remove the extra blank lines between #ACC004342-123 and More information: statements and print in the PDF. And in my code i have used document.add(new Paragraph(" ")); to just give one blank line only once..As the content is dynamic as i said we are getting those extra blank lines between ACC and More information: lines which we need to handle at our side, remove those extra lines(in betwen ACC and more information: statements) and print in the PDF but i dont want to remove all the blank lines which i have in between other lines.
Maybe the answer to your question is very simple. Maybe you are making the question too complex.
What do you think of this approach:
final DocumentLinesType documentLines = myDocument.getDocumentLines();
List<String> lines = documentLines.getDocumentLine();
String line;
for (int i = 0; i < lines.size(); ) {
line = lines.get(i);
document.add(new Paragraph(12, lines.get(i), font));
i++;
if (line.contains("#ACC")) {
while (lines.get(i).isEmpty())
i++;
}
}
I am assuming that the getSingleLine()
method returns some kind of list. I don't know, but since you use for (final String singleLine : myDocument.getSingleLine())
, it's a fair assumption.
As soon as a line is encountered that contains "#ACC", you skip all the empty lines until a line is encountered that isn't empty. Now you don't have to remove any blank lines, they are all skipped.
Update:
Try this example if you want proof that your question is not an iText question:
public static void main(String[] args) {
List<String> lines = new ArrayList<String>();
lines.add("This is first line.");
lines.add("This is second line.");
lines.add("");
lines.add("This is third printed line.");
lines.add("");
lines.add("This is fourth printed line.");
lines.add("");
lines.add("#ACC004342-123");
lines.add("");
lines.add("");
lines.add("");
lines.add("");
lines.add("");
lines.add("More information:");
lines.add("This is fifth printed line.");
lines.add("#ACC004342-123");
lines.add("");
lines.add("This is Sixth printed line.");
lines.add("Some information goes here.");
String line;
for (int i = 0; i < lines.size(); ) {
line = lines.get(i++);
System.out.println(line);
if (line.contains("#ACC")) {
while (lines.get(i).isEmpty()) i++;
}
}
}
The output of this code looks like this:
This is first line.
This is second line.
This is third printed line.
This is fourth printed line.
#ACC004342-123
More information:
This is fifth printed line.
#ACC004342-123
This is Sixth printed line.
Some information goes here.
Now there are no longer any blank lines after a line containing "#ACC#", but all the other blank lines are preserved.
If you want to preserve all the blank lines after a line with "#ACC" in case there is no "More information line:", you need to make a small adjustment:
public static void main(String[] args) {
List<String> lines = new ArrayList<String>();
lines.add("This is first line.");
lines.add("This is second line.");
lines.add("");
lines.add("This is third printed line.");
lines.add("");
lines.add("This is fourth printed line.");
lines.add("");
lines.add("#ACC004342-123");
lines.add("");
lines.add("");
lines.add("");
lines.add("");
lines.add("");
lines.add("More information:");
lines.add("This is fifth printed line.");
lines.add("#ACC004342-123");
lines.add("");
lines.add("This is Sixth printed line.");
lines.add("Some information goes here.");
String line;
for (int i = 0; i < lines.size(); ) {
line = lines.get(i++);
System.out.println(line);
int j = i;
if (line.contains("#ACC")) {
while (lines.get(j).isEmpty()) j++;
if (lines.get(j).contains("More information"))
i = j;
}
}
}
Now the output looks like this:
This is first line.
This is second line.
This is third printed line.
This is fourth printed line.
#ACC004342-123
More information:
This is fifth printed line.
#ACC004342-123
This is Sixth printed line.
Some information goes here.