Page 1 of 1

Linebreaks in Strings

PostPosted: Fri Sep 27, 2013 1:30 am
by bloodsoil
Hey guys,

I got some problem that I don't really understand.

I created a template in Word 2010 with few placeholders and I want to fill in them with some strings. Some of those string include linebreaks.

Everytime I just print this string in console (via System.out.print), it's looks like:
- 1
- 2
- 3
(by the way.. it's not a list or something. This string comes from a SQL DB)

Anytime I fill a placeholder with DOCX4J it looks completly wrong like:
- 1 - 2 - 3

I don't know what's wrong here. I hope it's enough informations to give me some tips. ;)

Re: Linebreaks in Strings

PostPosted: Sun Sep 29, 2013 7:29 pm
by jason
You need to use <w:br/> to represent a line break.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    Br br = wmlObjectFactory.createBr();
    r.getContent().add( br);
 
Parsed in 0.023 seconds, using GeSHi 1.0.8.4


Try the webapp (see menu above), to generate sample code from a sample docx.

Re: Linebreaks in Strings

PostPosted: Tue Oct 08, 2013 12:34 am
by bloodsoil
Thanks for it!

But it doesn't work. I forgot to add one important information.

This string:
-1
-2
-3

is ONE string. Not three.

Should I split it every time I found "\n" and then add the linebreak ?

Re: Linebreaks in Strings

PostPosted: Tue Oct 08, 2013 7:56 am
by jason
bloodsoil wrote:Should I split it every time I found "\n" and then add the linebreak ?


Yes

Re: Linebreaks in Strings

PostPosted: Mon Oct 14, 2013 10:21 pm
by bloodsoil
I still don't get it... Maybe I just don't really understand the problem. I try to explain it one more

I created a template with few placeholders in MS Word and try to replace them with some strings. The string are actually a result of SQL queries. Some of them ale very long and include line breaks:
-1
-2
-3

I already tried to replace "\n" with theoretical XML syntax for a line breaks but it doesn't works.

Here ist the code:
Code: Select all
public static void main(String args[]) {
      final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
      SQLResultForStaff SQL = new SQLResultForStaff();

      try {

         map.put("projectName", "test");
         map.put("headerName", "Test \n Test ");
         map.put("proposedPosition", "Test");
         map.put("nameOfStaff", SQL.SQLRes.get(1));
         map.put("strProfession", SQL.SQLRes.get(2));
         map.put("dayOfBirth", SQL.SQLRes.get(3));
         map.put("withFirmYear", SQL.SQLRes.get(4));
         map.put("nationality", SQL.SQLRes.get(5));
         map.put("memberInProsSoc", SQL.SQLRes.get(6));
         map.put("keyQualifications", Temp.tues(Temp.test));


         // C:\\test\\template1.docx is the template file
         WordprocessingMLPackage template = WordprocessingMLPackage
               .load(new File("C:\\CV\\templateDOCX.docx"));
         
         List texts = template.getMainDocumentPart().getJAXBNodesViaXPath(
               XPATH_TO_SELECT_TEXT_NODES, true);

         for (Object obj : texts) {

            Text text = (Text) ((JAXBElement) obj).getValue();
            String textValue = text.getValue();
            for (Object key : map.keySet()) {
               textValue = textValue.replaceAll("" + key + "",
                     (String) map.get(key));
            }
            text.setValue(textValue);
         }

         template.save(new File("C:\\CV\\" + SQL.SQLRes.get(1) + "_CV.docx"));
         
         writeToXML();
         SQL.SQLRes.clear();
         System.out.println("Saved!");
      } catch (Exception e) {
         e.printStackTrace();
         System.out.println("Errors: " + e.getCause());
      }
   }


And the code for replacing "\n"

Code: Select all
public class Temp {

   public static String test;
   // private static String[] split;
   private static String temp = "";

   public static void main(String[] args) {
      SQLResultForStaff SQL = new SQLResultForStaff();

      test = SQL.SQLRes.get(7);
      tues(test);

      // Splitted(test);
   }

   public static String tues(String test) {
      SQLResultForStaff SQL = new SQLResultForStaff();

      test = SQL.SQLRes.get(7);

      test = test
            .replaceAll(
                  "\n",
                  "</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val=\"BodyText\"/><w:keepNext/><w:keepLines/><w:spacing w:before=\"0\" w:after=\"0\"/></w:pPr>");

      System.out.println(test);
      return "<w:r><w:t>" + test;

   }
}



How should it looks like:
- Waste water treatment plants
- Flue gas cleaning facilities
- Emission monitoring facilities
- Fuel oil treatment
- Sampling systems for water/steam cycle


How does it looks like now:
- Waste water treatment plants - Flue gas cleaning facilities - Emission monitoring facilities - Fuel oil treatment - Sampling systems for water/steam cycle

Re: Linebreaks in Strings

PostPosted: Mon Oct 14, 2013 10:49 pm
by jason
Its a bit of a hack, but the VariableReplace example using XmlUtils.unmarshallFromTemplate should let you do something like what is described in the answer at http://stackoverflow.com/questions/1932 ... ing-docx4j

As a general approach, I generally recommend content control databinding instead. That'll handle your line breaks; it also supports a more general concept of repeating data.

Re: Linebreaks in Strings

PostPosted: Tue Oct 15, 2013 2:14 am
by bloodsoil
Hi Jason,

That sounds good!
Do you know any tutorial how can I handle with it ?

Re: Linebreaks in Strings

PostPosted: Wed Oct 16, 2013 8:35 pm
by bloodsoil
Hi Jason,

I think it works now.
But I have another problem:
I have few of this placeholders in my template but only one changes when I start it. Here is my code

Code: Select all
String itemId1 = "{5602763c-f2ee-4fde-b3e5-6bd35ce904e2}"
               .toLowerCase();
         CustomXmlDataStoragePart customXmlDataStoragePart = template
               .getCustomXmlDataStorageParts().get(itemId1);
         String itemId2 = "{4da4e256-e70d-4a0a-8e5e-7a36522f3d94}"
               .toLowerCase();
         customXmlDataStoragePart = template.getCustomXmlDataStorageParts()
               .get(itemId2);
         String itemId3 = "{d5a528ed-2e39-47c5-9b0a-1bc3bc743ecd}"
               .toLowerCase();
         customXmlDataStoragePart = template.getCustomXmlDataStorageParts()
               .get(itemId3);
         String itemId4 = "{d5a528ed-2e39-47c5-9b0a-1bc3bc743ecd}"
               .toLowerCase();
         customXmlDataStoragePart = template.getCustomXmlDataStorageParts()
               .get(itemId4);

         CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart
               .getData();

         ((CustomXmlDataStorageImpl) customXmlDataStorage)
               .setNodeValueAtXPath(
                     "/ns0:KeyQual[1]/ns0:keyQualifications[1]",
                     SQL.SQLRes.get(7), "xmlns:ns0='KeyQual'");

         

         ((CustomXmlDataStorageImpl) customXmlDataStorage)
               .setNodeValueAtXPath("/root[1]/header[1]",
                     SQL.SQLRes.get(1), null);

         customXmlDataStorage = customXmlDataStoragePart.getData();

         ((CustomXmlDataStorageImpl) customXmlDataStorage)
               .setNodeValueAtXPath(
                     "/ns0:ProfEdu[1]/ns0:ProfEduT[1]/@text",
                     SQL.SQLRes.get(8), "xmlns:ns0='ProfEdu'");

         ((CustomXmlDataStorageImpl) customXmlDataStorage)
               .setNodeValueAtXPath(
                     "/ns0:ProfEdu[1]/ns0:ProfEduY[1]/@year",
                     Temp.split[1], "xmlns:ns0='ProfEdu'");



and here how does it looks like in DOCX file:
Image

Re: Linebreaks in Strings

PostPosted: Fri Oct 18, 2013 12:01 am
by bloodsoil
Solved!

The problem was the different itemID of each placeholder. With a kind of container for other placeholders I can now edit more of them at the same time...