Colors in trouble in odt format

Hi,

there is a mistake with colors of shading for odt. A little test example

public static void main(String[] args)
{
    try
    {
        LicenseReader.getLizenz();
        testColor();
        testReadColor();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

private static void testColor() throws Exception
{
    // Open the template document.
    Document doc = new Document();
    DocumentBuilder db = new DocumentBuilder(doc);

    db.writeln("Erster Absatz");
    db.writeln("Zweiter Absatz");

    int i = 0;
    for (com.aspose.words.Section sec: doc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setForegroundPatternColor(Color.red);
                System.out.println(p.getText() +" red Foreground "+ shading.getForegroundPatternColor() +" Background "+ shading.getBackgroundPatternColor());
            } else {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setBackgroundPatternColor(Color.blue);

                System.out.println(p.getText() +" blue Foreground "+ shading.getForegroundPatternColor() +" Background "+ shading.getBackgroundPatternColor());

            }
            i++;
        }
    }

    doc.save(path + "color_test.odt", SaveFormat.ODT);
}

private static void testReadColor() throws Exception
{
    // Open the template document.
    Document doc = new Document(path + "color_test.odt");
    for (com.aspose.words.Section sec: doc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();

            System.out.println(p.getText() +" Foreground "+ shading.getForegroundPatternColor() +" Background "+ shading.getBackgroundPatternColor());

        }
    }
}

Output with SaveFormat.ODT

Erster Absatz
red Foreground java.awt.Color[r=255,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]
Zweiter Absatz
blue Foreground java.awt.Color[r=0,g=0,b=0] Background java.awt.Color[r=0,g=0,b=255]
red Foreground java.awt.Color[r=255,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]

Erster Absatz
Foreground java.awt.Color[r=0,g=0,b=0] Background java.awt.Color[r=255,g=0,b=0]
Zweiter Absatz
Foreground java.awt.Color[r=0,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]
Foreground java.awt.Color[r=0,g=0,b=0] Background java.awt.Color[r=255,g=0,b=0]

Output with only differenz in SaveFormat.DOC

Erster Absatz
red Foreground java.awt.Color[r=255,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]
Zweiter Absatz
blue Foreground java.awt.Color[r=0,g=0,b=0] Background java.awt.Color[r=0,g=0,b=255]
red Foreground java.awt.Color[r=255,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]

Erster Absatz
Foreground java.awt.Color[r=255,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]
Zweiter Absatz
Foreground java.awt.Color[r=0,g=0,b=0] Background java.awt.Color[r=0,g=0,b=255]
Foreground java.awt.Color[r=255,g=0,b=0] Background java.awt.Color[r=0,g=0,b=0]

Which I also expected for odt. The the odt-document opened with OpenOffice is black and not blue.
The information about blue is gone. And the red-Color could only be found in Background after saving it, expected would be Foreground.

I need to set the backgroundColor for doc and odt and after saving it need to be possible to reset the paragraph-shading.

Tanks.

Hi Babett,

Thanks for your inquiry. I have managed to reproduce the same issue with ODT file at my side. I have logged this issue as WORDSNET-7241 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

ForegroundPatternColor works correctly with Doc/Docx files. Please use the latest version of Aspose.Words for Java.

We apologize for your inconvenience.

Hi Babett,

Thanks for your patience. It is to update you that our development team has finished working on your issue (WORDSNET-7241) and has come to a conclusion that your issue and the behaviour you’re observing is actually not a bug in Aspose.Words. There is no direct analogue of Shading.ForegroundPatternColor in ODT formats. Microsoft Word too loses foreground color during ODT import/export. So, we’ve closed this issue as ‘Not a Bug’. If we can help you with anything else, please feel free to ask.

Best regards,

Hi,

the problem I have is with background. Which I need to set and afterwarts reset. But it is always the setted forground color which is re-read as background color. Who does this? The foreground is never the less not the color I would set.

Any ideas. I think this question is not fully asked yet.

Thanks Babett

Hi Babett,

Thanks for your inquiry. It would be great if you please share some more information about your query along with sample test document.

Please see the attached image. I have pointed three colors 1) Background color, 2) Highlighted color and 3) Font Color. Aspose.Words returns the correct background color for attached ODT file. Please check the following code snippet with attached ODT document.

Document doc = new Document(MyDir + "Test01.odt");
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true);
System.out.println(para.getParagraphFormat().getShading().getBackgroundPatternColor());
doc.save(MyDir + "AsposeOut.docx");
Document doc2 = new Document(MyDir + "AsposeOut.docx");
Paragraph para2 = (Paragraph) doc2.getChild(NodeType.PARAGRAPH, 0, true);
System.out.println(para2.getParagraphFormat().getShading().getBackgroundPatternColor());

Hi Babett,

Thanks for your inquiry. It would be great if you please share some more information about your query along with sample test document.

Please see the attached image. I have pointed three colors 1) Background color, 2) Highlighted color and 3) Font Color. Aspose.Words returns the correct background color for attached ODT file. Please check the following code snippet with attached ODT document.

Document doc = new Document(MyDir + "Test01.odt");
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true);
System.out.println(para.getParagraphFormat().getShading().getBackgroundPatternColor());
doc.save(MyDir + "AsposeOut.docx");
Document doc2 = new Document(MyDir + "AsposeOut.docx");
Paragraph para2 = (Paragraph) doc2.getChild(NodeType.PARAGRAPH, 0, true);
System.out.println(para2.getParagraphFormat().getShading().getBackgroundPatternColor());

Hi Babett,

Thanks for your inquiry. It would be great if you please share some more information about your query along with sample test document.

Please see the attached image. I have pointed three colors 1) Background color, 2) Highlighted color and 3) Font Color. Aspose.Words returns the correct background color for attached ODT file. Please check the following code snippet with attached ODT document.

Document doc = new Document(MyDir + "Test01.odt");
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true);
System.out.println(para.getParagraphFormat().getShading().getBackgroundPatternColor());
doc.save(MyDir + "AsposeOut.docx");
Document doc2 = new Document(MyDir + "AsposeOut.docx");
Paragraph para2 = (Paragraph) doc2.getChild(NodeType.PARAGRAPH, 0, true);
System.out.println(para2.getParagraphFormat().getShading().getBackgroundPatternColor());

Hi Tahir,

that is not the mentions issue. There is no problem in this example.

He a complete junit test.

The testForegroundDOC and testBackgroundDOC-Methodes show the expexted behaviour.

With the knowledge that ODT does not support ForegroundColor the testForegroundODT methode result is also expected.

The testBackgroundODT methode should evaluate to true if the issue is solved.

The testBackgoundODTconfusing shows an effect which I think give a hint to the underlying Bug. I don’t the this is the expected behavior.

Thanks Babett

import java.awt.Color;
import java.io.File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Paragraph;
import com.aspose.words.ParagraphCollection;
import com.aspose.words.SaveFormat;
import com.aspose.words.Shading;
import com.aspose.words.TextureIndex;

import de.allris.common.data.business.util.StringUtils;
import de.allris.server.office.aspose.AsposeBridge;
/***** 
**ColorOOTest.java**
***** 
*** Description: 
*** 
***** 
**Copyright: Copyright © 2009 CC e-gov GmbH**
***** 
**Company: CC e-gov GmbH**
*** 
* @author** `**Babett Schalitz**`
***
*/
public class ColorOOTest
{
    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        AsposeBridge.getLicense();
    }

/** 
* @throws java.lang.Exception *
 */
@AfterClass
public static void tearDownAfterClass() throws Exception
{}

private File tempFolder;

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception
{
    tempFolder = new File(System.getProperty("java.io.tmpdir") + File.separator + "allris_" + StringUtils.getRandomString(10));
    tempFolder.mkdir();
    tempFolder.deleteOnExit();
}

/** 
* @throws java.lang.Exception *
*/
@After
public void tearDown() throws Exception
{} 

@Test
public void testForgroundDOC() throws Exception
    {
        // Open the template document.
        Document doc = new Document();
        DocumentBuilder db = new DocumentBuilder(doc);

        db.writeln("Erster Absatz");
        db.writeln("Zweiter Absatz");

        int i = 0;
        for (com.aspose.words.Section sec: doc.getSections())
        {
            // EinfΣrben der smartDoc-Bereiche
            ParagraphCollection pcol = sec.getBody().getParagraphs();
            for (Paragraph p: pcol)
            {
                Shading shading = p.getParagraphFormat().getShading();
                if (i % 2 == 0)
                {
                    shading.setTexture(TextureIndex.TEXTURE_SOLID);
                    shading.setForegroundPatternColor(Color.red);
                }
                else
                {
                    shading.setTexture(TextureIndex.TEXTURE_SOLID);
                    shading.setForegroundPatternColor(Color.blue);
                }
                i++;
            }
        }

        File tempFile = new File(tempFolder.getAbsolutePath(), "color_test_fg.doc");
        tempFile.deleteOnExit();

        doc.save(tempFolder.getAbsolutePath() + File.separator + "color_test_fg.doc", SaveFormat.DOC);
        // Open the template document.
        Document reloaddoc = new Document(tempFolder.getAbsolutePath() + File.separator + "color_test_fg.doc");

        i = 0;
        for (com.aspose.words.Section sec: reloaddoc.getSections())
        {
            // EinfΣrben der smartDoc-Bereiche
            ParagraphCollection pcol = sec.getBody().getParagraphs();
            for (Paragraph p: pcol)
            {
                Shading shading = p.getParagraphFormat().getShading();
                if (i % 2 == 0)
                {
                    Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_SOLID, shading.getTexture());
                    Assert.assertEquals("Foreground Pattern red", Color.red, shading.getForegroundPatternColor());
                }
                else
                {
                    Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_SOLID, shading.getTexture());
                    Assert.assertEquals("Foreground Pattern blue", Color.blue, shading.getForegroundPatternColor());
                }
                i++;
            }
        }
}

@Test
public void testBackgroundDOC() throws Exception
{
    // Open the template document.
    Document doc = new Document();
    DocumentBuilder db = new DocumentBuilder(doc);

    db.writeln("Erster Absatz");
    db.writeln("Zweiter Absatz");

    int i = 0;
    for (com.aspose.words.Section sec: doc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setBackgroundPatternColor(Color.red);
            }
            else
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setBackgroundPatternColor(Color.blue);
            }
            i++;
        }
    }

    File tempFile = new File(tempFolder.getAbsolutePath(), "color_test_bg.doc");
    tempFile.deleteOnExit();

    doc.save(tempFolder.getAbsolutePath() + File.separator + "color_test_bg.doc", SaveFormat.DOC);
    // Open the template document.
    Document reloaddoc = new Document(tempFolder.getAbsolutePath() + File.separator + "color_test_bg.doc");

    i = 0;
    for (com.aspose.words.Section sec: reloaddoc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_SOLID, shading.getTexture());
                Assert.assertEquals("Background Pattern red", Color.red, shading.getBackgroundPatternColor());
            }
            else
            {
                Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_SOLID, shading.getTexture());
                Assert.assertEquals("Background Pattern blue", Color.blue, shading.getBackgroundPatternColor());
            }
            i++;
        }
    }
}

/**
* There is no direct analogue of Shading.ForegroundPatternColor in ODT formats.
* Therefore the Foreground Color is not set. Everything ok.
* hrows Exception
*/
@Test
public void testForgroundODT() throws Exception
{
    // Open the template document.
    Document doc = new Document();
    DocumentBuilder db = new DocumentBuilder(doc);

    db.writeln("Erster Absatz");
    db.writeln("Zweiter Absatz");

    int i = 0;
    for (com.aspose.words.Section sec: doc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setForegroundPatternColor(Color.red);
            }
            else
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setForegroundPatternColor(Color.blue);
            }
            i++;
        }
    }

    File tempFile = new File(tempFolder.getAbsolutePath(), "color_test_fg.odt");
    tempFile.deleteOnExit();

    doc.save(tempFolder.getAbsolutePath() + File.separator + "color_test_fg.odt", SaveFormat.ODT);
    // Open the template document.
    Document reloaddoc = new Document(tempFolder.getAbsolutePath() + File.separator + "color_test_fg.odt");

    i = 0;
    for (com.aspose.words.Section sec: reloaddoc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_NONE, shading.getTexture());

                // Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_SOLID, shading.getTexture());
                // Assert.assertEquals("Foreground Pattern red", Color.red, shading.getForegroundPatternColor());
            }
            else
            {
                Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_NONE, shading.getTexture());

                // Assert.assertEquals("Solid Texture", TextureIndex.TEXTURE_SOLID, shading.getTexture());
                // Assert.assertEquals("Foreground Pattern blue", Color.blue, shading.getForegroundPatternColor());
            }
            i++;
        }
    }
}
/*** THis test should evalute to true if mentioned issue would be solved.
* @throws Exception
*/
@Test
public void testBackgroundODT() throws Exception
    {
        // Open the template document.
        Document doc = new Document();
        DocumentBuilder db = new DocumentBuilder(doc);

        db.writeln("Erster Absatz");
        db.writeln("Zweiter Absatz");

        int i = 0;
        for (com.aspose.words.Section sec: doc.getSections())
        {
            // EinfΣrben der smartDoc-Bereiche
            ParagraphCollection pcol = sec.getBody().getParagraphs();
            for (Paragraph p: pcol)
            {
                Shading shading = p.getParagraphFormat().getShading();
                if (i % 2 == 0)
                {
                    shading.setTexture(TextureIndex.TEXTURE_SOLID);
                    shading.setBackgroundPatternColor(Color.red);
                }
                else
                {
                    shading.setTexture(TextureIndex.TEXTURE_SOLID);
                    shading.setBackgroundPatternColor(Color.blue);
                }
                i++;
            }
        }

        File tempFile = new File(tempFolder.getAbsolutePath(), "color_test_bg.odt");
        tempFile.deleteOnExit();

        doc.save(tempFolder.getAbsolutePath() + File.separator + "color_test_bg.odt", SaveFormat.ODT);
        // Open the template document.
        Document reloaddoc = new Document(tempFolder.getAbsolutePath() + File.separator + "color_test_bg.odt");

        i = 0;
        for (com.aspose.words.Section sec: reloaddoc.getSections())
        {
            // EinfΣrben der smartDoc-Bereiche
            ParagraphCollection pcol = sec.getBody().getParagraphs();
            for (Paragraph p: pcol)
            {
                Shading shading = p.getParagraphFormat().getShading();
                if (i % 2 == 0)
                {
                    Assert.assertEquals("Background Pattern red", Color.red, shading.getBackgroundPatternColor());
                }
                else
                {
                    Assert.assertEquals("Background Pattern blue", Color.blue, shading.getBackgroundPatternColor());
                }
                i++;
            }
        }
}

@Test
/**
* There is no direct analogue of Shading.ForegroundPatternColor in ODT formats. But why move the setting og the Color to the Background information,
*/
public void testBackgroundODTconfusing() throws Exception
{
    // Open the template document.
    Document doc = new Document();
    DocumentBuilder db = new DocumentBuilder(doc);

    db.writeln("Erster Absatz");
    db.writeln("Zweiter Absatz");

    int i = 0;
    for (com.aspose.words.Section sec: doc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setForegroundPatternColor(Color.red);
            }
            else
            {
                shading.setTexture(TextureIndex.TEXTURE_SOLID);
                shading.setForegroundPatternColor(Color.blue);
            }
            i++;
        }
    }

    File tempFile = new File(tempFolder.getAbsolutePath(), "color_test_bg.odt");
    tempFile.deleteOnExit();

    doc.save(tempFolder.getAbsolutePath() + File.separator + "color_test_bg.odt", SaveFormat.ODT);
    // Open the template document.
    Document reloaddoc = new Document(tempFolder.getAbsolutePath() + File.separator + "color_test_bg.odt");

    i = 0;
    for (com.aspose.words.Section sec: reloaddoc.getSections())
    {
        // EinfΣrben der smartDoc-Bereiche
        ParagraphCollection pcol = sec.getBody().getParagraphs();
        for (Paragraph p: pcol)
        {
            Shading shading = p.getParagraphFormat().getShading();
            if (i % 2 == 0)
            {
                Assert.assertNotSame("Background Pattern red", Color.red, shading.getBackgroundPatternColor());
            }
            else
            {
                Assert.assertNotSame("Background Pattern blue", Color.blue, shading.getBackgroundPatternColor());
            }
            i++;
        }
    }
}

}

Hi Babett,

Thanks for sharing the detail. The Shading.BackgroundPatternColor do not save correctly in ODT file format. I have logged this issue as WORDSNET-7589 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

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

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