Why does setting a color to a cell not work (Aspose Cells)? - excel

I have this code to try to set the background color of a cell (among other things):
private static readonly Color CONTRACT_ITEM_COLOR = Color.FromArgb(255, 255, 204);
. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.BackgroundColor = CONTRACT_ITEM_COLOR;
pivotTableSheet.Cells[4, 0].SetStyle(style);
The setting of horizontal and vertical alignment works, as does bold and height - everything but color:
What is yet needed? I have even tried setting ForegroundColor as well as Background colors, to :
style.ForegroundColor = Color.Red;
style.BackgroundColor = Color.Blue;
...but neither does anything - the cell still looks exactly the same as the screenshot above.

Please change your code segment to (see the highlighted lines):
e.g
Sample code:
. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
**style.ForegroundColor = CONTRACT_ITEM_COLOR;
style.Pattern = BackgroundType.Solid;**
pivotTableSheet.Cells[4, 0].SetStyle(style);
..........
it should work fine.
I am working as Support developer/ Evangelist at Aspose.

Sometimes setting background works, sometimes it doesn't. Aspose is full of bugs -- ClosedXML is much more reliable but harder to use. Wish I would not have spent the money on a product that was developed by third party in third world county.
var cells = worksheet.Cells; //get cells collection from active worksheet <br>
var srcCells = workbook.Worksheets[1].Cells;<br>
for (int i = 0; i<rowCount; i++)<br>
{<br>
var srcStyle = srcCells[i, 0].GetStyle();<br>
var destStyle = cells[i, 0].GetStyle();<br>
destStyle.Pattern = BackgroundType.Solid;<br>
destStyle.ForegroundColor = Color.FromArgb(srcStyle.ForegroundArgbColor);<br>
cells[i, 0].SetStyle(destStyle);<br>
}<br>
Above Code does not work. srcStyle Foreground color argb is 170,215,255.
Debugging code destStyle ForegroundColor is set to 170,215,255 but when saved as xlsx all the cell backgrounds are white.
In ClosedXML code the following code works perfectly
worksheet.Row(i).Cell(0).Style.BackgroundColor = Color.FromArgb(argb)
Conclusion: Save $$$$$ and use ClosedXML

enter image description here
all is fine only ForegroundColor is not showing

Related

How do I change font sizes in Heaps?

I'm currently coding in Haxe with Heaps and was checking on how to display text. One thing I want to do is give different font sizes to different texts. Naturally, I declared two Font instances and resized one of them. However, both texts are resized and I cannot manage to have them resize independently. How should I resize font sizes in Heaps?
class Main extends hxd.App{
override function init(){
var font : h2d.Font = hxd.res.DefaultFont.get();
var font2 : h2d.Font = hxd.res.DefaultFont.get();
font2.resizeTo(23);
var tf = new h2d.Text(font);
var tf2 = new h2d.Text(font2);
tf.text = "Hello World\nHeaps is great!";
tf.textColor = 0x00FF00;
tf.x = 100;
tf.y = 100;
tf.textAlign = Center;
tf2.text = "Hello World\nHeaps is great!";
tf2.textColor = 0x00FF00;
tf2.x = 300;
tf2.y = 300;
tf2.textAlign = Center;
s2d.addChild(tf);
s2d.addChild(tf2);
static function main(){
new Main();
}
}
The approach taken does not work because DefaultFont.get() caches the result.
You could either:
Copy the second font by doing var font2 : h2d.Font = font.clone() so that it gets its own properties.
Adjust scaleX and scaleY of Text.

How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)?

I have an image that is embedded in my solution and is used on the main form of a Winforms app, and also for pasting into a spreadsheet. The image size is 156X121.
I put it on the sheet like so:
var ms = new MemoryStream();
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Yet when it is on the sheet, it stretches out and spills into neighboring cells, partially obscuring other data:
As you can see, the size is no longer 156X121. The height has been increased by 25%. Why? And how can I prevent that?
This code:
MessageBox.Show(string.Format("image height is {0}", _logo.Height));
MessageBox.Show(string.Format("image width is {0}", _logo.Width));
...showed me "126" as the height and "151" as the width, matching the image as it is in the project. So why is the original size changed? Is there a property I can set to leave the size alone and not stretch it? Or how can I prevent this gumbification of the image?
It's bizarre to me that the image is one size (126X151), its original size is purported to be 1.26" X 1.63", and its size after being scaled is 1.57" X 1.63".
Who or what is allowing this 25% increase in Height?
NOTE: If I select the "Reset" button in the image's "Size and Properties" dialog, it shrinks up as I want it to be, setting the Height "back" to 100% from 125%. Is there a way to do this "Reset" programmatically?
UPDATE
Based on the answer, I tried this:
var ms = new MemoryStream();
//_logo.Height = 121; <= cannot set; Height is a readonly property
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = pivotTableSheet.Pictures[0];
//Workbook.Worksheets[0].Pictures[0]; <= does not compile
pic.HeightScale = 100;
pic.WidthScale = 100;
(Workbook.Worksheets[0] does not compile for me).
It makes no difference; the image is still stretching vertically.
UPDATE 2
I realized I needed "Workbook" to be "workBook" due to this:
private static Workbook workBook;
...and so I tried this:
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable and subsequently gets hidden, so need to use 1
pic.Height = 121;
pic.WidthScale = 100;
...but it still gumbifies the image vertically. So does replacing "pic.Height = 121" with "pic.HeightScale = 100;"
So this is the code currently, which adds the image, but in a vertically gumbified manner:
var ms = new MemoryStream();
//_logo.Height = 121; readonly
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable
//pic.Height = 121;
pic.HeightScale = 100;
pic.WidthScale = 100;
Please use this code to reset it to original height.
Picture pic = wb.Worksheets[0].Pictures[0];
pic.HeightScale = 100;
pic.WidthScale = 100;
Note: I am working as Developer Evangelist at Aspose

Change color instead of background color of text in etherpad

I am trying to find solution for "Authorship color" in etherpad. People who never used etherpad, please ignore this question as they will not understand.
So normally "Authorship Color" is the color provided as a background-color of the text and it can be given as parameter when initializing the pad. It helps recognizing who has written what in pad.
I want to have white background for all text and change the text-color instead of background color depending upon the user. So its like if I provided red color when initializing the pad, I want to have red writing in pad instead of red background and white writing in pad(as usual).
Please dont put this question on hold as I dont have any specific code to provide related to this problem. Ask in comment instead, i will clear whatever is not understandable.
Thanks,
First of all, Thanks to everyone for not blocking or making this question on Post as no piece of code was provided.
I was about to put bounty on this question when I decided to try myself one more time and well I have resolved the problem after going through a lot. Though, it helped me understand etherpad better.
I want to list two important links related to etherpad:
https://github.com/ether/etherpad-lite/wiki
About src - https://github.com/ether/etherpad-lite/wiki/Introduction-to-the-source
They can be very important if you are trying to understand etherpad or want to do some modification yourself.
So here is how you do it:
In src/static/js/ace2_inner.js , go to setAuthorStyle function and Replace:
// author color
authorStyle.backgroundColor = bgcolor;
parentAuthorStyle.backgroundColor = bgcolor;
// text contrast
if(colorutils.luminosity(colorutils.css2triple(bgcolor)) < 0.5)
{
authorStyle.color = '#ffffff';
parentAuthorStyle.color = '#ffffff';
}else{
authorStyle.color = null;
parentAuthorStyle.color = null;
}
// anchor text contrast
if(colorutils.luminosity(colorutils.css2triple(bgcolor)) < 0.55)
{
anchorStyle.color = colorutils.triple2css(colorutils.complementary(colorutils.css2triple(bgcolor)));
}else{
anchorStyle.color = null;
}
With:
authorStyle.backgroundColor = '#ffffff';
parentAuthorStyle.backgroundColor = '#ffffff';
authorStyle.color = bgcolor;
parentAuthorStyle.color = bgcolor;
And comment Out:
if ((typeof info.fade) == "number")
{
bgcolor = fadeColor(bgcolor, info.fade);
}
Ofcource dont forget to restart process by bin/run.sh for changes to take place.
People, who are interested to understand how it works can keep reading.
So etherpad receives parameters with which etherpad has been initialized in src/static/js/pad.js so if you have defined: 'userColor' when you have initialized the pad, it will go in globalUserColor in mentioned file.
Then this variable globalUserColor populates pad.myUserInfo.colorId in same file.
Now in collab_client.js , this colorId gets stored in cssColor in function tellAceAuthorInfo and editor.setAuthorInfo is being called by giving parameter bgcolor: cssColor.
Now this function setAuthorInfo exists in src/static/js/ace2_inner.js which calls to other native function(same file) setAuthorStyle where we have made changes.
What I did is that instead of changing background color with provided variable bgcolor which actually holds userColor :
authorStyle.backgroundColor = bgcolor;
parentAuthorStyle.backgroundColor = bgcolor;
I changed backgroundColor to white(#ffffff) and color of text to bgcolor :
authorStyle.backgroundColor = '#ffffff';
parentAuthorStyle.backgroundColor = '#ffffff';
authorStyle.color = bgcolor;
parentAuthorStyle.color = bgcolor;
I also deleted:
// text contrast
if(colorutils.luminosity(colorutils.css2triple(bgcolor)) < 0.5)
{
authorStyle.color = '#ffffff';
parentAuthorStyle.color = '#ffffff';
}else{
authorStyle.color = null;
parentAuthorStyle.color = null;
}
// anchor text contrast
if(colorutils.luminosity(colorutils.css2triple(bgcolor)) < 0.55)
{
anchorStyle.color = colorutils.triple2css(colorutils.complementary(colorutils.css2triple(bgcolor)));
}else{
anchorStyle.color = null;
}
Because, these lines set the contrast of text color depending upon the background chosen color. But I have made background white and set text color to given userColor so I don't need the contrast functionality anymore.
Ultimately I also commented:
if ((typeof info.fade) == "number")
{
bgcolor = fadeColor(bgcolor, info.fade);
}
Because it makes the text fade when user is not online, at least for me it did.
So that's it. I hope it will help someone who wants same functionality as I did.

iTextSharp pdf table cell height issue

I have iTextSharp 5.4.4 (nuget) and have a nice table with a barcode (ean13) and text below it.
I have specific tablecell heights (and cell widths) because I want to print the pdf to an A4 with stickers.
Here is the current layout:
as you can see, there is a rather large gap between the ean13 code and the text below.
here is my C# code:
PdfPCell c = new PdfPCell();
c.FixedHeight = 21.2f * postScriptPointsPerMilimeter; // to get to accurate milimeters
c.HorizontalAlignment = Element.ALIGN_CENTER;
Paragraph p = new Paragraph();
p.Font.Size = 6;
Chunk code = new Chunk(dr["productcode"].ToString());
p.Alignment = Element.ALIGN_CENTER;
p.Add(code);
BarcodeEAN ean13 = new BarcodeEAN();
ean13.CodeType = BarcodeEAN.EAN13;
ean13.Code = dr["ProductEan13"].ToString();
ean13.BarHeight = 4.0f * postScriptPointsPerMilimeter;
var a = ean13.CreateImageWithBarcode(cb, null, null);
a.ScalePercent(90);
c.AddElement(a);
c.AddElement(p);
t.AddCell(c);
My question is to reduce the space between the barcode and the text. I cannot see if it has something to do with the barcode's margins or the paragraph's or maybe both... hard to troubleshoot.
p.Leading = 0;
That was missing. I thought that
p.SpacingBefore = 0;
would do the trick, but it didn't. Leading did!

XNA 4.0 weird color error

I am having problems drawing a terrain with XNA, specifically with colors (It happens with VertexPositionColorNormal and with VertexPositionTextureNormal). My code is following:
public BasicEffect GetEffectForColoredTerrain()
{
this.coloredTerrainEffect.EnableDefaultLighting();
this.coloredTerrainEffect.SpecularPower = 0.01f; //Power of the light
this.coloredTerrainEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f); //Color of the light when it reflects on a surface
this.coloredTerrainEffect.EmissiveColor = new Vector3(1, 0, 0);
this.coloredTerrainEffect.DirectionalLight0.Enabled = true; //Enable directional light
this.coloredTerrainEffect.DirectionalLight0.DiffuseColor = (new Vector3(0.2f, 0.2f, 0.2f)); //Diffuse color
this.coloredTerrainEffect.DirectionalLight0.SpecularColor = (new Vector3(0.2f, 0.2f, 0.2f)); //Specular color
this.coloredTerrainEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1, -1f, 1)); //Direction where the light comes from.
this.coloredTerrainEffect.View = Camera.GetInstance().GetViewMatrix();
this.coloredTerrainEffect.Projection = Camera.GetInstance().GetProjectionMatrix();
this.coloredTerrainEffect.Alpha = (float)((float)Configuration.GetInstance().TerrainOpacity / (float)100);
this.coloredTerrainEffect.VertexColorEnabled = true;
return this.coloredTerrainEffect;
}
And this code for drawing terrain:
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
WorldContent.CommonGraphicsDevice.RasterizerState = rs;
//Restore things that SpriteBatch can have overriden
WorldContent.CommonGraphicsDevice.BlendState = BlendState.AlphaBlend;
WorldContent.CommonGraphicsDevice.DepthStencilState = DepthStencilState.Default;
WorldContent.CommonGraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
BasicEffect shader = ShadersHandler.GetInstance().GetEffectForColoredTerrain();
foreach (EffectPass pass in shader.CurrentTechnique.Passes)
{
pass.Apply();
WorldContent.CommonGraphicsDevice.Indices = indexBufferVertices;
WorldContent.CommonGraphicsDevice.SetVertexBuffer(vertexBufferVertices);
WorldContent.CommonGraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexPositionColorNormalList.Length, 0, indicesVertices.Length / 3);
}
However, the result is really weird, as you can see in the following images:
Terrain laterally
Terrain bottom
Color is a gradient from Yellow to white (yellow up, white down). However, if I use the effects file from Riemers tutorial (effects.fx from 3D Series 1), everything is correct, as you can see here:
Terrain good laterally
Terrain good bottom
If you wish, you can see the effect code here: Effects file
So QUESTION: Does anyone knows what is happening here with BasicEffect? I would like to use the Riemers file (everything seems correct with it), but I need to use transparency and the BasicEffect object provides me alpha property, which is perfect for what I am looking for.
PD: The same problem happens with textured terrain, using VertexPositionNormalTexture
Looking at the images, the discoloration appears to be a very nice circle, as from a directional light. I would try removing the DirectionalLight0 property settings you are using in the BasicEffect example and see if that corrects the discoloration.

Resources