0

I am using itext API in java to create and print the dynamic content in the PDF document.

Below is the dynamic content which i need to add in the PDF document.

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.

In the above shown sample text, their is a gap between #ACC004342-123 and More information: lines which i need to remove.

    public static File createInformationPdf(final List<MyDocumentType> content) throws AccServiceException
    {
        Document document = null;
        FileOutputStream fos = null;

        try
        {
             final String prefix = "letter";
        final File myPDF = File.createTempFile(prefix, ".pdf");
        document = new Document(PageSize.LETTER);

        fos = new FileOutputStream(temporaryPDF);

        PdfWriter.getInstance(document, fos);
        document.open();

        Font font = new Font(Font.FontFamily.COURIER,10);
   for (final MyDocumentType myDocument : content)
        {
            if (myDocument.getDocumentines() != null)
            {
                final DocumentLinesType documentLines = myDocument.getDocumentLines();
                for (final String line : documentLines.getDocumentLine())
                {
          document.add(new Paragraph(line,font));
                }
            }
  }
            document.close();
            return temporaryPDF;
        }
        catch (Exception e)
        {
         //log the exception
        }

    }

PS: Above is my java code, i just want to remove the more number of blank lines which are present between #ACC004342-123 and More information: lines and just give one blank line between them. As i am getting that content from webservice call i need to handle in my java code. Hope iam clear now. Thanks.

1
  • This is a better description than your previous question. Granted: you now admit that you're a dirty coder, but at least now we can help you and explain what you're doing wrong. We now understand what is causing the unwanted blank lines. Sep 8, 2016 at 6:00

3 Answers 3

1

It seems that you want to get some extra space every time a line containing the #ACC is added. However, based on your previous question limited content, we also know that you want to avoid that this extra white space is added if the line containing #ACC is the last line on a page.

You add a blank line by adding an extra Paragraph. That's not a good idea, because that means a blank line is always added, even in cases where you don't want it.

It's much better to use the setSpacingAfter() method. For instance:

float spaceBetweenLines = 12;
Paragraph p;
for (final String singleLine : document.getSingleLine()) {
    p = new Paragraph(spaceBetweenLines, singleLine, font);
    if (singleLine.contains("#ACC")) {
        p.setSpacingAfter(spaceBetweenLines);
    }
    if (!singleLine().isEmpty())
        document.add(p);
}

This code is much easier to read and understand than your code where you set a flag for a reason that is very hard to understand for anyone who reads your code (or who has to maintain your code).

Actually: it is still very hard to understand your question, because you are adding a blank line and at the same time asking to remove a blank line. It is very hard for a third party to understand why you are adding the blank line in the first place.

The beauty of using a method such as setSpacingAfter() is that it only adds the extra space when necessary. If the Paragraph is the last paragraph of the document, no space is added. If it's the last paragraph before a new page, no space is added (and you don't have unnecessary extra space on the next page, which was the problem you described in your previous question limited content).

Or maybe I'm misinterpreting this question. Maybe you want to add some extra space before a paragraph. In that case, you can use the setSpacingBefore() method:

float spaceBetweenLines = 12;
Paragraph p;
for (final String singleLine : document.getSingleLine()) {
    p = new Paragraph(spaceBetweenLines, singleLine, font);
    if (singleLine.contains("#ACC")) {
        p.setSpacingBefore(spaceBetweenLines);
    }
    if (!singleLine().isEmpty())
        document.add(p);
}

In this case, extra space is added before every line that contains #ACC, unless that line is the first line on a page. In that case, no unwanted white space is added.

If this doesn't answer your question, please rephrase your question. It is already more clear than your previous question, but a third party still has to do a lot of guess work in order to fully understand what you're asking.

4
  • 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. @BrunoLowagie
    – javaUser
    Sep 8, 2016 at 13:56
  • So, if I understand you well, you want "#ACC004342-123" and "More information:" to be on the same line? If that is not a correct interpretation: which lines need to be removed? Where do those lines come from? If you don't want them, why are you adding them in the first place? You are still being unclear. Sep 8, 2016 at 14:24
  • Please see my dynamic content text shown in my question. I am getting that content from webservice call. And if you notice "#ACC004342-123" and "More information:" there are more number of blank lines between them. I just want to remove those more blank lines and just give one blank line between those two statements and remaining content should print as it is. JUST WANT TO REMOVE THOSE EXTRA BLANK LINES BETWEEN "#ACC004342-123" and "More information:" . i don't want them to be in same line, just remove those more number of blank lines. @Bruno Lowagie.
    – javaUser
    Sep 8, 2016 at 14:41
  • OK, I posted another answer. Why did you mark this as an iText question? Your question is almost too simple to ask on Stack Overflow. It is totally unrelated to iText, but you made your question so complex that no one could answer it. Sep 8, 2016 at 14:44
0

The flag overcomplicates your logic and its use isn't clear. I think the below code should do what you want.

The if statement checks if it's the '#ACC' line. If it is is passes the document to another method which skips all empty lines and returns the first non empty one. This is the 'More information' line in your case.

If the '#ACC' line is not found it just prints the line as normal.

public void removeMultipleEmptyLines(Document document){
    for (final String singleLine : document.getSingleLine()) {
        if (singleLine.contains("#ACC")) {
            document.add(new Paragraph(" ")); //adding one blank line
            String nextNonEmptyLine = getNextNonEmptyLine(Document document);//this should return the information line
            document.add(new Paragraph(12, nextNonEmptyLine, font));
        }else{
            document.add(new Paragraph(12, singleLine, font));              
        }
    }
}

private String getNextNonEmptyLine(Document document) {
    String singleLine = document.getSingleLine();
    while(singleLine.trim().length() == 0){
        singleLine = document.getSingleLine();
    }
    return singleLine;
}
5
  • getSingleLine() is not a method form Document..its our internal method which no where belongs to Document object. @ColonelCustard
    – javaUser
    Sep 8, 2016 at 13:53
  • I assumed Document was your own internal class as you didn't specify and thats how you were using it when you first posted the question.
    – Tom Hanley
    Sep 8, 2016 at 18:14
  • Yes, sorry about that.Now i cleaned up my code in my post. As suggested above without comparing with static content like if(singleLine.contains("#ACC")) or any other static string, can we just blindly check if we have more than 2 blank lines just remove those additional blank lines.As i just noticed there are scenarios without these strings in the dynamic content.It can allow 2 blank lines between each printed line but not more than that. Because in my dynamic content sometimes there are one blank line and sometimes 2 blank lines between paragraph which need to be allowed.@ColonelCustard
    – javaUser
    Sep 8, 2016 at 19:32
  • So sorry, you want to make sure that if there is more than one blank line between any line, that this gets reduced to one?
    – Tom Hanley
    Sep 8, 2016 at 19:44
  • There can be one or two blank lines between each printed lines which i am getting. If u see the post above, the content i gave, between ACC and More information lines there are many blank lines which i need to remove and in future there may be blank lines any where randomly as i am getting that content from webservice call.So i just want to remove blank lines if there are more than 2.And i don't want to check with any static text as we done earlier.I am using java 5 version for my development.
    – javaUser
    Sep 8, 2016 at 19:57
0

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.
5
  • Please see my original code editied and added in my question above..Now i am confused as how can i place your code in my code.You want me to remove the inner for each loop and try with your code..sorry for asking too many questions.. @Bruno Lowagie
    – javaUser
    Sep 8, 2016 at 15:02
  • The thing that is really annoying is that you are asking the wrong questions. What is content? Is it a List of DocumentType? What is DocumentType and is DocumentLinesType? What does the getDocumentLine() return? Is it a List<String>? You sound like a troll who tries to provoke angry reactions from Stack Overflow users. Why did you call yourself javaUser? Sep 8, 2016 at 15:29
  • Iam sorry for the trouble..May be i am not able to express what i exactly wanted..As i am getting the content from webservice and i thought i should make question easier thats the reason not added all the return type..To make it simple please see my edited java code, and see the content,i just wanted to remove those more number of blank lines present in between #ACC and More information: lines..thanks. @Bruno Lowagie
    – javaUser
    Sep 8, 2016 at 15:33
  • Please see my edited posted.Hope that is clear now.thanks
    – javaUser
    Sep 8, 2016 at 15:43
  • I have updated my answer. If this answers your question, you have wasted a lot of time: your employer's time as well as mine. Sep 8, 2016 at 16:25

Your Answer

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

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