I thought of that but it changes the proportions as well. Anyways, thanks a lot for your help!
@Adhirath
Setting the same higher horizontal and vertical resolution will result in larger pixels in the width and height of the image, while maintaining the same aspect ratio The physical size of the image (in unit of points) remains unchanged
thanks, will try that out
@Adhirath
You are welcome. Please take your time to try the suggested solutions. Hopefully, your issue will be sorted out. Please let us know your feedback.
Emf image is vector image, the line may be variable if zoom is different. Please check the attchement word document comparing Aspose generated Emf image and Excel Copied “As shown when printed” image.
EmfComparing.docx (21.4 KB)
Hi guys, i am facing another issue-
I am using aspose cells to take an image of a range, the image is not looking like the excel range exactly, it varies in column width, row width, overall image size etc. the code i use to get the range image is the following-
byte[]? originalImage = null;
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Emf,
};
SheetRender originalSr = new SheetRender(range.Worksheet, originalImgOptions);
using (MemoryStream originalMs = new MemoryStream())
{
originalSr.ToImage(0, originalMs);
originalImage = originalMs.ToArray();
}
return originalImage;
the excel range is under table 2 in this attachment -
Book_1742996484 (1).zip (2.2 MB)
and after pasting it look like this(top one) in comparison to a copy pasted image(below one)-
image.png (27.7 KB)
this is the print preview image-
image.png (2.9 KB)
for a better understanding have a look at the last page of this document-
Document_1744033388.zip (5.3 MB)
@Adhirath
Would you like to provide a runnable test code? If we could provide screenshots and highlight the error location, it would be very helpful for us to locate the issues. We will check it soon.
There are two options when you “Copy as Picture” in Excel: “As shown on screen” and “As shown when printed”.
Excel_Copy_Picture.png (146.9 KB)
The image generated by the code you shared is close to the one by copying “As shown when printed” in Excel.
If you want the generated image to be close to the one by copying “As shown on screen” in Excel, please enable ImageOrPrintOptions.OnlyArea
option.
Hi John, sorry this is as much code as I can share but it has all the logic for getting the image. I have provided the screenshots and also the source documents to show the difference. Let me know what will help you understand it better
Hi I tried it out but still getting the exact same image. i replaced OnePagePerSheet = true with what you suggested. is there some other change needed as well?
I can see some issues in the generated image. e.g. words space issue. To make us be clear about your issues, please share us a screenshot that highlight the issue points in the generated image.
Thanks for your shared screenshot. Your issue is clear now.
Please try the following options for ImageOrPrintOptions
, it will get good result.
ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
TextRenderingHint = TextRenderingHint.AntiAlias,
OnePagePerSheet = true,
ImageType = ImageType.OfficeCompatibleEmf,
};
outputTable.zip (3.5 KB)
thank you soo much, but I’m not able to add TextRenderingHint is it available only in the latest version?
The TextRenderingHint is only avaliable for windows target framework. If your project is running on Windows platform, please change TargetFramework to windows, e.g. change net8.0
to net8.0-windows
.
Hi guys, is there a way to find out if a cell belongs to a range? so if I have a cell C4 and I want to find out if it is part of a named range how can i do that?
@Adhirath
Please first obtain the named range by its name, and obtain the starting row, starting column, ending row, and ending column of the named range. Then obtain the row and column indexes of the cell, and compare them with the row and column indexes of the range to determine whether the cell is included in the range.
The sample code as follows:
Workbook workbook = new Workbook("book1.xlsx");
// Getting the specified named range
Range range = workbook.Worksheets.GetRangeByName("TestRange");
int startRow = range.FirstRow;
int startColumn = range.FirstColumn;
int endRow = range.FirstRow + range.RowCount - 1;
int endColumn = range.FirstColumn + range.ColumnCount - 1;
Worksheet sheet = range.Worksheet;
Cell c4 = workbook.Worksheets[0].Cells["C4"];
bool isInRange = false;
if (
c4.Worksheet.Name == sheet.Name
&& c4.Row >= startRow
&& c4.Row <= endRow
&& c4.Column >= startColumn
&& c4.Column <= endColumn
)
{
isInRange = true;
}
Console.WriteLine("c4 is in range: " + isInRange);
@Adhirath
There’s no good solution to check whether the cell belongs to which named range. We have to iterate all defined name to check it as the following codes:
private static Range Check(Workbook workbook, int row, int column)
{
foreach (Name name in workbook.Worksheets.Names)
{
Range range = name.GetRange();
if (range != null)
{
if (row >= range.FirstRow && row < range.FirstRow + range.RowCount
&& column >= range.FirstColumn && column < range.FirstColumn + range.ColumnCount)
{
return range;
}
}
}
return null;
}
This method is very inefficient, if you need to check cell many times, you should gather all defined named ranges at first.
And if you have other issues, please add a new post. Too many questions in a post will confuse us.
Will do, sorry about that!
Yes, please refer to the suggested code and write your own code for your needs to accomplish your task. In case you find any issue, kindly do provide details and sample code, we will check it soon.