Results 1 to 12 of 12
  1. #1
    s.errani is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default How can I do to justify a text in a paragraph?

    Good day.

    I'm using Apache POI 3.15 version in order to create a Microsoft Word document in Java.
    I cannot found a way in order to justify a text in a paragraph.
    In XWPFParagraph class I found only BOTH (of ParagraphAlignment class), but with it, if the are a few words in a line it adds several spaces between the words and this is very wrong.
    How can I do to make it?

    Best regards.

    Stefano Errani
    Last edited by s.errani; 04-16-2017 at 09:44 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How can I do to justify a text in a paragraph?

    A SSCCE would be nice.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    s.errani is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: How can I do to justify a text in a paragraph?

    Quote Originally Posted by SurfMan View Post
    A SSCCE would be nice.
    When the words are small longer, BOTH property insert some spaces between words in order to align the text to the left and right margins and it's very ugly to see.
    This is an example:
    How can I do to justify a text in a paragraph?-esempio_both.jpg
    Last edited by s.errani; 04-16-2017 at 06:02 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How can I do to justify a text in a paragraph?

    Quote Originally Posted by s.errani View Post
    When the words are small longer, BOTH property insert some spaces between words in order to align the text to the left and right margins and it's very ugly to see.
    This is an example:
    Click image for larger version. 

Name:	esempio_both.jpg 
Views:	15 
Size:	11.0 KB 
ID:	7284
    That's not an SSCCE. We need to see some code that compiles, that demonstrates the problem, that we can use to try something for ourselves.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    s.errani is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: How can I do to justify a text in a paragraph?

    This is a snippet of code:
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph par = doc.createParagraph();
    par.setAlignment(ParagraphAlignment.BOTH);
    String str = "Allo scadere del termine il contratto si intenderà automaticamente decaduto e risolto.";
    XWPFRun run = par.createRun();
    run.setText(str);
    run.setFontFamily("Times New Roman");
    run.setFontSize(11);
    run.addBreak();
    Last edited by s.errani; 04-17-2017 at 09:21 AM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: How can I do to justify a text in a paragraph?

    How can that code be compiled and executed for testing?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How can I do to justify a text in a paragraph?

    Works for me when you leave out the addBreak, and create new paragraphs every time. That's how it's actually supposed to work even.
    Java Code:
    package com.wj;
    
    import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Doc {
        public static void main(String[] args) {
            XWPFDocument doc = new XWPFDocument();
            String[] text = new String[]{
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus lacinia dui nisl. Pellentesque dapibus enim ac vulputate efficitur. Quisque vitae arcu nisi. Aenean sit amet elementum nibh. Vestibulum hendrerit massa eu dui sodales facilisis. Donec ullamcorper, nisi a tincidunt ullamcorper, arcu ligula fermentum enim, ut interdum odio erat sed dolor. Praesent vitae pharetra urna, at vestibulum dui. ",
                    "Curabitur magna lacus, dignissim eu nulla id, pretium pulvinar elit. Ut sit amet imperdiet turpis. Nam ipsum sem, vulputate eget lacus ac, dapibus convallis sapien. Integer quis dignissim turpis. Sed ac est id nunc efficitur luctus eget eget arcu. Curabitur enim mauris, elementum a enim quis, ultrices posuere enim. Maecenas ut ante vel nisl pulvinar rhoncus eu a felis. Proin in venenatis quam. Cras sodales et nulla eu sollicitudin. Vestibulum non rhoncus lacus, sit amet pharetra urna. Aliquam faucibus felis in lacinia efficitur. Phasellus ut leo elementum metus dapibus porta ut id eros. Aenean sollicitudin purus vel vulputate aliquet. Morbi in ornare ante, nec congue justo. Quisque mauris justo, mollis ac mollis eget, posuere quis orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. ",
                    "Vivamus fermentum sed felis in efficitur. Sed aliquam enim ornare accumsan pharetra. Cras mattis ornare nisi in laoreet. Morbi vel laoreet dui. Praesent varius pellentesque nisl, sed lacinia ipsum placerat et. Sed volutpat orci a augue fringilla dictum. Sed tempus sagittis augue a pellentesque. Aliquam fringilla vel mauris congue viverra. ",
            };
    
            for (String s : text) {
                XWPFParagraph par = doc.createParagraph();
                par.setAlignment(ParagraphAlignment.BOTH);
                XWPFRun run = par.createRun();
                run.setText(s);
                run.setFontFamily("Times New Roman");
                run.setFontSize(11);
            }
    
            try (FileOutputStream out = new FileOutputStream("simple.docx")) {
                doc.write(out);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    How can I do to justify a text in a paragraph?-lorem.png
    Last edited by SurfMan; 04-17-2017 at 03:13 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    s.errani is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: How can I do to justify a text in a paragraph?

    Quote Originally Posted by Norm View Post
    How can that code be compiled and executed for testing?
    I'm using NetBeans IDE 8.1 with J2SE 8 (compiled for Java 7 because on web site there is J2SE 7) and Tomcat 7.
    The sent code is present in a servlet.
    I think that BOTH property run correctly, but in the library is missing JUSTIFY property (the one that serves me, and that is used in most of the commercial documents).
    I don't know if there is an alternative way to do this the same.

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How can I do to justify a text in a paragraph?

    Quote Originally Posted by s.errani View Post
    I'm using NetBeans IDE 8.1 with J2SE 8 (compiled for Java 7 because on web site there is J2SE 7) and Tomcat 7.
    The sent code is present in a servlet.
    I think that BOTH property run correctly, but in the library is missing JUSTIFY property (the one that serves me, and that is used in most of the commercial documents).
    I don't know if there is an alternative way to do this the same.
    I guess you missed my solution then. Ah well...

    For each paragraph, you create a new paragraph, not a break.
    Last edited by SurfMan; 04-17-2017 at 03:44 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  10. #10
    s.errani is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: How can I do to justify a text in a paragraph?

    Quote Originally Posted by SurfMan View Post
    I guess you missed my solution then. Ah well...

    For each paragraph, you create a new paragraph, not a break.
    I answered you before to read yours with code.
    I've just applied your solution in my case and all run correctly.
    In order to insert a blank row between two paragraphs I've included one without the text.

    Best regards.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How can I do to justify a text in a paragraph?

    If you need space between paras, that's a formatting issue isn't it?
    I don't know the Word part of POI (it's never got out of the sandbox), but that's how Word works.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How can I do to justify a text in a paragraph?

    I've been teaching Word/Excel to companies two decades back. You'll be amazed how many people, still today, will hit enter so many times that they get the next paragraph on the next page. And hitting the spacebar just enough times to get text to center. They will be asking why their text is moving all around when they insert a new paragraph somewhere.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. Replies: 0
    Last Post: 02-28-2014, 11:33 AM
  2. Replies: 2
    Last Post: 06-12-2013, 01:54 PM
  3. How to left justify and wrap text
    By Tejas in forum New To Java
    Replies: 12
    Last Post: 10-05-2011, 12:27 AM
  4. Replies: 4
    Last Post: 02-05-2011, 06:47 PM
  5. Help with Format.justify method
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •