We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Getting pageColor inconsistently

Hi,

I created a new blank document in MSWord. I used the following code to take the pagecolor of the document. I faced two problem.

Scenario 1:

Document doc = new Document(“color.docx”);
Color color = doc.getDocument().getPageColor();
String backColor = “rgb(”+color.getRed()+","+color.getGreen()+","+color.getBlue()+")";

System.out.println(“Page Color =” +backColor);

I got the output as ‘Page Color = rgb(0,0,0)’ which is black but the document pagecolor is white.

Scenario 2:
When I changed the document color manually to some other colors, the above code gives the correct result for the document formats(docx,doc,rtf) but not for odt format.

Please help me in this scenarios and clarify me if i am wrong any where.

Hi Vignesh,

Thanks for your inquiry.

Could you please attach both documents here for testing? I will then take a closer look into this for you.

Thanks,

Hi,

Thanks for your reply. Please find the documents below.

1.For the first document(color.docx), i am getting rgb(0,0,0) but the original color is white.

2.For the first document(pagecolor.odt) , i am getting rgb(0,0,0) but the original color is grey.

Could you clarify me the way i can get the exact pagecolor for all formats in all scenarios.



Hi Vignesh,

Thanks for attaching your documents here.

The color returned for the first document is correct. The alpha component of the color is "0" meaning that the color is transparent, and not black. You can find the alpha component by using the getAlpha() method on the retrieved color.

If you look in your document in MS Word and you will see that the page background is set to "None" meaning that there is no background color set and the returned color is correct. MS Word displays a document with a transparent background as white in any case.

Regarding the incorrect color returned for your .ODT document, I managed to reproduce the issue on my side. Your request has been linked to the appropriate issue. We will inform you as soon as it is resolved.

Thanks,

Hi,

Thanks for your reply.
I have created a document with pagecolor as black, so the default text color automatically appeared as white in MSWord.

Process1: Getting the pageColor

Document doc=new Document(“backcolor_black.docx”);
Color color = doc.getDocument().getPageColor();
String backColor = “rgb(”+color.getRed()+","+color.getGreen()+","+color.getBlue()+")";
System.out.println(“page Color = “+backColor );

Result:
backColor = rgb(0,0,0)


Process2: Setting the pageColor to a new Document

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getDocument().getDocument().setPageColor(getColor(backColor));

public static Color getColor(String style) {
try {
if (style.contains(“rgb”)) {
String color = style.replace(“rgb”, “”);
color = color.replace(’)’, ’ ‘);
color = color.replace(’(’, ’ ');
color = color.trim();
int red = Integer.parseInt(color.substring(0, color.indexOf(”,”)));
System.out.println(“Red = " +red);
color = color.substring(color.indexOf(”,") + 1);
int green = Integer.parseInt(color.substring(0, color.indexOf(",")));
color = color.substring(color.indexOf(",") + 1);
int blue = Integer.parseInt(color);
System.out.println(new Color(red, green, blue));
return new Color(red, green, blue);
} else {
style = style.replace(’#’, ’ ').trim();
int intValue = Integer.parseInt(style, 16);
return new Color(intValue);
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, “Exception in setStyleforRun:::::”, e);
}
return null;
}
doc.save(“Color.doc”, SaveFormat.DOC);

I couldn’t get the same pagecolor in the newly created document(Color.doc).Please correct my code if there is anything wrong.


Hi,

please respond to my previous question.

Hi

Thanks for your request. I think, the problem occurs because you are ignoring alpha component of the color. I corrected your code. Please see the code below:

@Test

public void Test001() throws Exception {

Document doc = new Document("C:\\Temp\\backcolor_black.docx");

Color color = doc.getDocument().getPageColor();

String backColor = "rgb("+color.getRed()+","+color.getGreen()+","+color.getBlue()+","+color.getAlpha()+")";

System.out.println("page Color = "+backColor );

Document doc1 = new Document();

DocumentBuilder builder = new DocumentBuilder(doc1);

builder.getDocument().getDocument().setPageColor(getColor(backColor));

doc1.save("C:\\Temp\\Color.doc", SaveFormat.DOC);

}

public static Color getColor(String style) {

try {

if (style.contains("rgb")) {

String color = style.replace("rgb", "");

color = color.replace(')', ' ');

color = color.replace('(', ' ');

color = color.trim();

int red = Integer.parseInt(color.substring(0, color.indexOf(",")));

System.out.println("Red = " +red);

color = color.substring(color.indexOf(",") + 1);

int green = Integer.parseInt(color.substring(0, color.indexOf(",")));

color = color.substring(color.indexOf(",") + 1);

int blue = Integer.parseInt(color.substring(0, color.indexOf(",")));

color = color.substring(color.indexOf(",") + 1);

int alpha = Integer.parseInt(color);

System.out.println(new Color(red, green, blue, alpha));

return new Color(red, green, blue, alpha);

} else {

style = style.replace('#', ' ').trim();

int intValue = Integer.parseInt(style, 16);

return new Color(intValue);

}

} catch (Exception e) {

LOGGER.log(Level.WARNING, "Exception in setStyleforRun:::::", e);

}

return null;

}

Hope this helps.

Best regards,

Hi,

Thanks for your reply. But how can we set four parameters for rgb using,
<span style=“font-size: 10pt; font-family: “Courier New”;”>String backColor = “rgb(”+color.getRed()+","+color.getGreen()+","+color.getBlue()+","+color.getAlpha()+")";

Hi

Thanks for your inquiry. Sorry, did not correct this string. It should look like this:

String backColor ="rgba("+color.getRed()+","+color.getGreen()+","+color.getBlue()+","+color.getAlpha()+")";

Also, could you please explain why you need to convert a color to string in such way? Maybe there is a simpler way to achieve what you need.

Best regards,

Hi,



Thanks for ur reply. I want to construct a json object with all document defaults for our rendering like

JSONObject defaultStyles = new JSONObject();
PageSetup ps = doc.getSections().get(0).getPageSetup();

double pgMarginRight = ConvertUtil.pointToInch(ps.getRightMargin());
double pgMarginLeft = ConvertUtil.pointToInch(ps.getLeftMargin());
double pgMarginTop = ConvertUtil.pointToInch(ps.getTopMargin());
double pgMarginBottom = ConvertUtil.pointToInch(ps.getBottomMargin());

defaultStyles.put(“ds_ml”, pgMarginLeft + “in”);
defaultStyles.put(“ds_mr”, pgMarginRight + “in”);
defaultStyles.put(“ds_mt”, pgMarginTop + “in”);
defaultStyles.put(“ds_mb”, pgMarginBottom + “in”);

Color color = doc.getDocument().getPageColor();
String backColor = “rgb(” + color.getRed() + “,” + color.getGreen() + “,” + color.getBlue() + “,” + color.getAlpha() + “)”;
defaultStyles.put(“ds_backcolor”, backColor); (either in rgb fomrat or in # format)

Can you give a simpler way of taking the exact page color without doing such string manipulations.

Hi

Thank you for additional information. If you need to create a json object, then you need to convert color to string, because json s string format data.

Just use the method I suggested previously, i.e. include alpha into the string.

Best regards,

Hi,

Thanks for your reply. But i am using the same method for getting and setting font back-color. Its working well. Then why for page color alone i have to use alpha?

Hi Vignesh,

Thanks for your request. I think, you should take Alpha in account in all cases where you get/set color. You did not encounter any problems with font color because you did not encounter a case where Alpha is needed. However, you cannot guaranty that Alpha components will not be needed in all cases.

Best regards,

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


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