Problems with right-aligned cells when exported to PDF

Hi

I just found something weird in my export.

I use Aspose.Cells to create a Excel file. I also save it as a PDF file.

That file has some fields that are aligned to the right. They appear correctly in Excel. When the very same file is exported to a PDF file, very often, the content is cut at very weird place (looks like it cut on the column width).

You will find attach my Excel file, the xml to generate it and the outputed PDF. Look at page 9 of the PDF for an example (should read "Asset Under Management:" and "Bank Proprietary:")

Hi,

I have tested your file and found that the error is caused by the xml which is generated by Aspose.Cells. If you look in the xml file you can see that the "Asset U" is in one cell and "nder Management:" is in another. I am moving this to the Aspose.Cells forum so that the Aspose.Cells team can take a look at it.

Thanks.

Hi,

We will figure out the issue soon.

Thanks for for being patient!

Hi,

Please try this fix.

In this fix,we use Cell.GetStyle and SetStyle method replace the property of Cell.Style. The two methods will save more memory usage than cell.Style property.

If you want to set style of the cell, please change your codes as :

//Getting the style of the cell.

Style style = cell.GetStyle();

//Setting the property of the style.

.......

//Setting the style of the cell

cell.SetStyle(style);

With the previous version, I was doing something like this:

With .Style

.Copy(mWorkbook.Styles(pBaseStyle))

.HorizontalAlignment = TextAlignmentType.Left

.Borders(Aspose.Cells.BorderType.RightBorder).LineStyle = CellBorderType.Thin

.Custom = "#,##0.0_);[Red](#,##0.0)"

.HorizontalAlignment = TextAlignmentType.Right

End With

I have replace this with but the .Copy method doesn’t seems to work anymore:

objStyle = .GetStyle

With objStyle

.Copy(mWorkbook.Styles(pBaseStyle))

.HorizontalAlignment = TextAlignmentType.Left

.Borders(Aspose.Cells.BorderType.RightBorder).LineStyle = CellBorderType.Thin

.Custom = "#,##0.0_);[Red](#,##0.0)"

.HorizontalAlignment = TextAlignmentType.Right

End With

objStyle = .GetStyle

There is also another issue with predefined styles (which was working correctly before).

It seems that some properties (like VerticalAlignment) if I do this:

Dim objStyle As Aspose.Cells.Style

objStyle = mWorkbook.Styles(enuStyle.TableStrategyBorders)

Also when I modify the style for a cell, the formatting remains for the following cell. It wasn't like this before. It will be a pain if we need to change this everywhere.

Hi,

If you want to apply the style to the cell,please call Cell.SetStyle() method.

In the current fix, we do not keep the reference of Style object when you get style with Cell.GetStyle().

Does that means that the preset styles I create on the workbook are now completely useless?

Does that mean that I have to reset all style properties on each cell of my worksheet?

Do you have new samples on how to work with styles?

Hi,

We have found this problem that if we change the named style ,the style of the cells(which use this named style) do not be changed.

We will fix it soon.Thanks for your patience.

Hi

When do you think you will be able to send us a new DLL?

Hi,

Please try the fix (4.4.1.8) downloading it from the the thread: <A href="</A>.</P> <P>In this fix, we add a method Style.Update for your need. <FONT face=宋体>For a named style, if you want to change the Style, you may call Style.Update method. </FONT><FONT face=宋体>Otherwise the style of cell will not be changed. Style.Update method behaves like an "OK" button i.e., when you have done your modified with some Style, you may call it for final implementation.

Thank you.

Hi

Do we still have to use .GetStyle and .SetStyle or can we use the .Style property directly like we use to do in the past?

Do you call the .Style.Update method after the .SetStyle method?

Do you have an example handy? For example, say I want to give a cell a basic type and set borders around it. What would the code look like?

Hi,

Please try the attached version (4.4.1.9), it will resolve your issues. The attachment also contains styledemo with template excel file to better understand how you can use SetStyle, GetStyle methods of Cell class and where you can use Update method of the Style class.

Do we still have to use .GetStyle and .SetStyle or can we use the .Style property directly like we use to do in the past?

In this fix, we retain the Style property of the Cell class for the users. You may utilize Cell.Style property but using separatedly Cell.GetStyle and Cell.SetStyle certainly would be better choices for performance optimization in the long run.

Do you call the .Style.Update method after the .SetStyle method?

Please check the styledemo for reference, Cell.SetStyle() is used to apply a style to a cell whereas Style.Update method is used to modify an existing style (if you have already applied some style to a range of cells and you modify the style using Style.Update method, the style formattings of those cells would be upldated too.)

Do you have an example handy? For example, say I want to give a cell a basic type and set borders around it. What would the code look like?

Please check the styledemo for your reference.

Thank you.

Hi

Thanks for the fix. Seems to work very well so far.

It is still not clear for me when I have to use the Style.Update method as I didn't used it and it seems to work.

Hi emoreau,

If remove the line style.Update() in ChangePreDefinedNamedStyle demo, you will see "Percent" Style in Format|Style, the number format of Percent Style has been changed as "m/d/yyyy", but the number format of Range A1:C8 still is "0%" not "m/d/yyyy" in Ms Excel(The version is Excel 2003 sp3 in my machine).

In Ms Excel, if you change the "Percent" style in the book1.xls in the demoes, the style of range A1:C8 should be changed also.The method style.Update is used to change style of range A1:C8.

Could you try to remove style.Update() in that demo to check the number format of Range A1:C8?

You are right. I have the same behavior as you.

Is it possible that it only applies when you use predefined style? I don't use them as I always start from scratch.

Hi,

If you changed the named style after you have applied the named style to the cell from scratch, you still have to call Style.Update();

Please check the demo AddNamedStyle

Workbook workbook = new Workbook();

Style style = workbook.Styles[workbook.Styles.Add()];
style.Number = 14;
style.Font.Color = System.Drawing.Color.Red;
style.Name = "Date1";

Cells cells = workbook.Worksheets[0].Cells;
cells["A1"].SetStyle(style);
Range range = cells.CreateRange("B1", "D1");
StyleFlag flag = new StyleFlag();
flag.All = true;
range.ApplyStyle(style, flag);

//if the named style has been set to cells or range,
//you want to change it and change the style of the cells/range, please call style.Update().
style.Font.Color = System.Drawing.Color.Black;
style.Update();
workbook.Save(@"F:\FileTemp\book3.xls");

If you do not call style.Font.Color = System.Drawing.Color.Black after you set the style to cell, you don't need to call style.Update().

If I understand correctly (and I may be wrong), ApplyStyle is used when working with a Range object.

Since I always format on a cell basis, I set a style object and then affect it to a cell using the SetStyle method. In this scenario, do I ever need to call the Update method.

I understand that this feature is quite new but it would be a good time to update your online documentation and have a clear example of when to use ApplyStyle, SetStyle, and Update methods.

Hi,

1). Yes, ApplyStyle method is used to set a style to a range or worksheet cells.

2). Well, if you do't want to update / modify an existing style object (which you have already applied to some cell(s)), you do not need to utilize Style.Update() method.

3). We will soon update our documenatation with sample codes related newer APIs.

Thank you.

Hi,

We have supported to apply style to row/column/range/entire worksheet.

Cells.ApplyColumnStyle Apply style to the whole column.

Cells.ApplyRowStyle Apply style to the whole row.

Cells.ApplyStyle Apply style to the whole worksheet.

Range.ApplyStyle Apply style to the range.