Border for multiline text inserts border between each line

Hi Team,

When border is applied to a paragraph and a multiline text is written, a border is seen between the lines of the multiline texts. Is there any way to have a single border and the entire multiline text is completely within the border?

Below is the code I use to write a multiline text

builder.getParagraphFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.writeln("Hello World! \n Expecting this multiline to be under single border");

Thanks,
Kumar

------------------- Sample Code ----------------

import com.aspose.words.BuildVersionInfo;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.LineStyle;

public class BorderMultilineText
{
    
    public static void main(String[] args) throws Exception
    {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        writeBorderMultitext(builder, doc);
        save(doc);
        System.out.println("done");
    }
    
    private static void writeBorderMultitext(DocumentBuilder builder,
                                             Document doc) throws Exception
    {
        
        builder.getParagraphFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        builder.writeln("Hello World! \n Expecting this multiline to be under single border \n Entire text should be under same border instead of separate horizontal border for each line.");
        
        builder.writeln("This text is in the next complete border as expected.");
        builder.getParagraphFormat().clearFormatting();
        builder.writeln("Version is " + BuildVersionInfo.getVersion());
    }
    
    private static void save(Document doc) throws Exception
    {
        doc.save("C:\test_old.doc");
        doc.save("C:\test_old.pdf");
    }
}

To add on the issue, there is no border seen for PDF output. I had attached both word and doc outputs during the problem description.

Hi Kumaraswamy,

Thanks for sharing the details.

To set borders for a paragraph, you will have to set the TOP, BOTTOM, LEFT and RIGHT border styles correctly. Here is the code snippet which could be used inside your writeBorderMultitext function:

builder.getParagraphFormat().getBorders().getByBorderType(BorderType.LEFT).setLineStyle(LineStyle.SINGLE); 
builder.getParagraphFormat().getBorders().getByBorderType(BorderType.RIGHT).setLineStyle(LineStyle.SINGLE); 
builder.getParagraphFormat().getBorders().getByBorderType(BorderType.TOP).setLineStyle(LineStyle.SINGLE); 
builder.getParagraphFormat().getBorders().getByBorderType(BorderType.BOTTOM).setLineStyle(LineStyle.SINGLE);
builder.writeln("Hello World! \n Expecting this multiline to be under single border \n Entire text should be under same border instead of separate horizontal border for each line."); builder.getParagraphFormat().clearFormatting();
builder.writeln("");
builder.getParagraphFormat().getBorders().setLineStyle(LineStyle.WAVE); builder.writeln("This text is in the next complete border as expected.");
builder.getParagraphFormat().clearFormatting();
builder.writeln("Version is " + BuildVersionInfo.getVersion());

Hope this helps. Apart from this, I was able to reproduce the issue with paragraph border display when save to PDF is performed. I have logged this issue as WORDSNET-7706 in our issue tracking system. I have also linked this forum thread to the issue so that you will be notified via this thread as soon as the issue is resolved.

We apologize for inconvenience. Please do let us know if you have any more queries.

Thanks Muhammad for the quick reply and the code. It helps.

Hi Kumaraswamy,

A word from development team is that they have started work on the reported issue. They have also advised that a possible workaround could be to specify width of borders as follows:

builder.ParagraphFormat.Borders.Left.LineWidth = 1; 
builder.ParagraphFormat.Borders.Right.LineWidth = 1; 
builder.ParagraphFormat.Borders.Top.LineWidth = 1; builder.ParagraphFormat.Borders.Bottom.LineWidth = 1; 

Hope this helps. Please do let us know if you have any more queries.

The issues you have found earlier (filed as WORDSNET-7706) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi Muhammad,

Another issue I see is that the border breadth size is the width of the page not the width of the text.

However, if you use the below, the border is applied upto the width of the text, which is what I’m looking for. But this has another defect of having multiple borders for multiline text.

docBuilder.getFont().getBorder().setLineStyle(LineStyle.SINGLE);

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry.

Aspose.Words page layout engine tries to mimic the way the Microsoft Word’s page layout engine works. To you, this means that if you convert a Microsoft Word document into PDF, XPS or print it using Aspose.Words, the output will appear almost exactly as if it was done by Microsoft Word. Also, I would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link:
https://releases.aspose.com/words/net

I hope, this helps.

Best regards,

Hi Awais,

The above tests in my previous comment were run using Aspose.Words for Java v13.3. The issue is happening with the generated word document.

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry.

Please find attached an output PDF document that is generated on my side using Aspose.Words for Java. I am afraid, I could not see any issue with this output document, could you please clarify where the issue is? Also, please share the complete code to be able to reproduce this issue on my side.

PS: I generated this PDF from the test.doc you attached in your first post.

Best regards,

Hi Awais,

The concern is for both word and pdf document.

If I use builder.getFont().getBorder().setLineStyle(1); to set border for a multiline text, I see multiple border for multiple line (See test_2.doc and test_2.pdf).

If I use code as in writeBorderMultitext_1() method, I see single border, however, I see border width upto the page width. I want to limit the border width upto the text width (See test_1.doc and test_1.pdf).

What I’m looking for is that I see single border for multiline text and the width of the border should end where the text ends, not upto the page width.

Thanks,
Kumar

public static void main(String[] args) throws Exception
{
    Utils.setupLicense();
    Utils.printAsposeVersion();
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    writeBorderMultitext_1(builder, doc);
    save(doc);
    System.out.println("done");
}

private static void writeBorderMultitext_1(DocumentBuilder builder,
    Document doc) throws Exception
{

    builder.getParagraphFormat().getBorders()
        .getByBorderType(BorderType.LEFT)
        .setLineStyle(LineStyle.SINGLE);
    builder.getParagraphFormat().getBorders()
        .getByBorderType(BorderType.RIGHT)
        .setLineStyle(LineStyle.SINGLE);
    builder.getParagraphFormat().getBorders()
        .getByBorderType(BorderType.TOP).setLineStyle(LineStyle.SINGLE);
    builder.getParagraphFormat().getBorders()
        .getByBorderType(BorderType.BOTTOM)
        .setLineStyle(LineStyle.SINGLE);

    builder.write("Hello World! \n Expecting");
}

private static void writeBorderMultitext_2(DocumentBuilder builder,
    Document doc) throws Exception
{

    builder.getFont().getBorder().setLineStyle(1);
    builder.write("Hello World! \n Expecting");
}

private static void save(Document doc) throws Exception
{
    doc.save("C:\test_old.doc");
    doc.save("C:\test_old.pdf");
}

Hi Kumar,

Thanks for the additional information. In this case, you need to apply borders to the runs of text inside paragraph. Please see the following code change:
private static void writeBorderMultitext_1**(DocumentBuilder builder,**

Document doc) throws Exception
{
    builder.getFont().getBorder().setLineStyle(LineStyle.SINGLE);
    builder.writeln("Hello World!");
    builder.writeln("Expecting");
}

I hope, this helps.

Best regards,

Hi Awais,

I’m looking for something as in the screenshot.

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. Could you please create your target Word document using Microsoft Word and attach it here for our reference? I will investigate the structure of your target document and will provide you code to achieve the same using Aspose.Words.

Best regards,