简体   繁体   中英

remove extra blank lines

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.

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.

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM