While working on this, I thought back to what you said -
Since you are inserting barcode picture into some merged cell (e.g., A1:F1), so you have to first gather the width of each column involved, calculate the center place considering the image’s width/height intact (using your own logic) and then place the barcode image to the place accordingly.
If I do this, would I need to generate the barcode twice?
The way it seems, I’d have to do something like
- Generate barcode (so I can get the width) and calculate barcode width
- Calculate sheet width
- subtract barcode with from sheet width and divide by 2
- result of that is the left padding needed to make barcode centered
- remove 1st generated barcode
- regenerate barcode with specified padding
Since there isn’t a clear column that can be used as center. Is this right?
All the reports will have 1 barcode max, so this might be feasible.
I have the following code to try that, but I’m having an issue since if I use Sheet.Cells.GetColumnWidth though all my columns I can get the page width in “points” (1/72 of an inch) but the BarCodePicture.Width is in Pixels. How can I do the conversion between these?
One I can figure out the conversion, I figure I can do something similar to this:
Workbook Doc = new Workbook();
Worksheet Sheet = Doc.Worksheets[0];
//Merge barcode cell and add testing to get accurate page width
Sheet.Cells.Merge(12, 0, 1, 9);
Sheet.Cells["A1"].PutValue("Testing");
Sheet.Cells["J1"].PutValue("Testing");
//make first barcode to get width
Aspose.BarCode.Generation.BarcodeGenerator Generator = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.Code39Standard, "TestBarcodeFont");
Generator.Parameters.Resolution = 300;
Generator.Parameters.Barcode.CodeTextParameters.Location = Aspose.BarCode.Generation.CodeLocation.None;
//Create memorystream
MemoryStream BarCodeStream = new MemoryStream();
Generator.Save(BarCodeStream, Aspose.BarCode.Generation.BarCodeImageFormat.Bmp);
//Row 13 to place barcode
Aspose.Cells.Cell cell = Sheet.Cells["A13"];
//Place temporarily to get width
Aspose.Cells.Drawing.Picture BarCodePicture = Sheet.Pictures[Sheet.Pictures.Add(12, 0, BarCodeStream, 100, 100)];
//math to get left padding for centered barcode
int MaxCount = Sheet.Cells.MaxColumn;
double CurrentWidth;
double PageWidth = 0;
double SheetWidth = 0;
float PaddingSize;
for(int i=0; i<MaxCount; i++)
{
CurrentWidth = Sheet.Cells.GetColumnWidth(i);
SheetWidth = PageWidth + CurrentWidth;
PageWidth = SheetWidth;
}
//This is broken since PageWidth is Points and BarCodePicture.Width is in Pixels, I end up with a negative number that trunicates the barcode
PaddingSize = (float)((PageWidth - BarCodePicture.Width) / 2);
//Add padding to generator
Generator.Parameters.Barcode.Padding.Left.Point = PaddingSize;
//Create memorystream
MemoryStream BarCodeWithPaddingStream = new MemoryStream();
Generator.Save(BarCodeWithPaddingStream, Aspose.BarCode.Generation.BarCodeImageFormat.Bmp);
//Re-save the new barcode and place again in report - Need some way to remove first barcode
Generator.Save(BarCodeWithPaddingStream, Aspose.BarCode.Generation.BarCodeImageFormat.Bmp);
Aspose.Cells.Drawing.Picture BarCodePictureWithPadding = Sheet.Pictures[Sheet.Pictures.Add(12, 0, BarCodeWithPaddingStream, 100, 100)];
BarCodePictureWithPadding.Placement = Aspose.Cells.Drawing.PlacementType.FreeFloating;
//Set row height to barcode height
Sheet.Cells.SetRowHeightPixel(cell.Row, BarCodePictureWithPadding.Height);
//Set column width to barcode with WARNING: bad times in report if enabled :(
//Sheet.Cells.SetColumnWidthPixel(cell.Column, BarCodePicture.Width);
//Fit report to 1 page wide
Sheet.PageSetup.FitToPagesWide = 1;
Doc.Save(DataDir + " barcode test.xlsx");
It doesn’t work currently due to PaddingSize ending up negative since I’m comparing Points to pixels… also I can’t figure out how to remove the first picture either. I end up (understandably) with 2 barcodes in the file.
So my question is: Is there a way to get the ColumnWidth in pixels, or convert it? I know I’m probably not going to get the “point size” out of an image.
Also, how can I get the width without placing the barcode in the report OR remove it later once I have the width?
Thanks for your help.
EDIT: just saw your previous post. Thanks for the quick replies. I still have the two questions above though, thanks!