I am extracting content from an Excel sheet and inserting it into a Word document using the following method:
private static void InsertHtmlTable(DocumentBuilder builder, Workbook workbook, Worksheet worksheet, Range range, ReplacingArgs e)
{
string htmlContent = GetHtmlStream(workbook, worksheet, range);
builder.InsertHtml(htmlContent);
builder.Writeln();
}
The issue is that the content (print area) is quite wide, and I want it to fit properly on an A4 page in landscape orientation—without altering its appearance or layout.
Is there a way to ensure that the inserted content automatically fits within the landscape A4 page, preserving the visual structure and formatting?
large area from A1 till S411 for example that print area
@RaghulBabu214
To ensure that the content you are inserting from Excel fits properly on an A4 page in landscape orientation without altering its appearance, follow these steps:
-
Cause: The issue arises when the HTML table generated from Excel does not fit the landscape layout of an A4 page, leading to improper formatting.
-
Solution: You can adjust the width of the inserted HTML table to ensure it fits correctly.
-
Next Steps:
- Set Page Orientation: First, ensure your Word document is set to landscape orientation by modifying the
Document
properties.
- Adjust Table Width: Specify a width for the table that matches the A4 landscape page width, which is approximately 29.7 cm (or 842 points in Word).
- Insert HTML with Width: Modify your
GetHtmlStream
method to include a style attribute for the table width. For example, set the width to 100% to ensure it scales appropriately.
Here’s a modified version of your method:
private static void InsertHtmlTable(DocumentBuilder builder, Workbook workbook, Worksheet worksheet, Range range, ReplacingArgs e)
{
string htmlContent = GetHtmlStream(workbook, worksheet, range);
// Set the table width to 100% to fit the landscape A4 page
htmlContent = htmlContent.Replace("<table", "<table style='width:100%;'");
builder.InsertHtml(htmlContent);
builder.Writeln();
}
- Check Margins: Ensure that the margins of your Word document are set appropriately to maximize the usable space on the page.
By following these steps, you will successfully insert the content from Excel into your Word document while maintaining its visual structure and ensuring it fits within the landscape layout. If you have any further questions or need additional assistance, feel free to ask!
@RaghulBabu214,
Thank you for your inquiry. I will move your thread to the appropriate category, where one of our colleagues from Aspose.Words will review your requirements and assist you. If possible, it would be helpful if you could provide sample files (e.g., input and output files such as DOCX, XLSX, etc.) and complete code snippets along with a sample document showcasing your expected results. This will enable us to better understand your needs and address them soon.
@RaghulBabu214 I am afraid there is no easy way to achieve this. As you may know MS Word documents are flow by their nature and content is reflowed to pages on the fly. In your case when table data does not fit the page it is required to scale it. Unfortunately, content in MS Word document is not an image and cannot be scaled easily as whole.
You can try using Table.AutoFit
method to fit the table to window. But this operation might affect the original table layout.
@alexey.noskov Is there a way to convert a cell range (class in cell) into an image while preserving all formatting, including colors, and then insert that image into a Word document so that it fits properly? Additionally, is it possible to make the content within the image searchable in Word?
If making the image searchable is not possible, please provide code that converts a specified range into an image with full formatting retained.
I want to extract the range and paste it with Edit picture option enabled so that I can search so is there a way to achieve this using code if so can you please help me out with it
@RaghulBabu214 From Aspose.Words side, you can use DocumentBuilder.InsertImage method to insert images into MS Word documents.
Regarding conversion Excel to image you can find information here:
https://docs.aspose.com/cells/net/convert-excel-to-image/
@amjad.sahi Could you please help with code example for conversion a specified range into an image
Range
to Image conversion code can you please provide also insert it in Edit picture mode in APOSe word while insertion preserving its style and background colour all
I am trying with the above example
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Create workbook from source file.
Workbook workbook = new Workbook(sourceDir + “sampleExportRangeOfCellsInWorksheetToImage.xlsx”);
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Set the print area with your desired range
worksheet.PageSetup.PrintArea = “D8:G16”;
// Set all margins as 0
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
// Set OnePagePerSheet option as true
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = ImageType.Jpeg;
options.HorizontalResolution = 200;
options.VerticalResolution = 200;
// Take the image of your worksheet
SheetRender sr = new SheetRender(worksheet, options);
sr.ToImage(0, outputDir + “outputExportRangeOfCellsInWorksheetToImage.jpg”);
but for the below file background color is not preserve can you please help me to preserve that also
INPUT.zip (1.1 MB)
The zip contains Excel and image generated with the above code also after inserting this image is there a way to make it editable picture via code
@RaghulBabu214,
Thanks for the sample XLSM file and output image.
The issue with the background color of the worksheet cells not being preserved occurs because the “Black and White” option is enabled in the “Page Setup | Sheet” dialog tab for the worksheet “Sample_DOC” in your provided template XLSM file. Kindly disable this option to ensure that the background or shading colors are retained in the output image generated by Aspose.Cells. Please consider adding the following line to your code snippet.
// Disable "Black and white" option/setting in Page Setup|Sheet tab
worksheet.PageSetup.BlackAndWhite = false;
Kindly see and refer to the complete code snippet that I used with your sample Excel file which works fine and the output image is fine tuned.
// Create workbook from source file.
Workbook workbook = new Workbook("e:\\test2\\INPUT.xlsm");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Set all margins as 0
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
// Disable "Black and white" option/setting in Page Setup|Sheet tab
worksheet.PageSetup.BlackAndWhite = false;
// Set OnePagePerSheet option as true
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = ImageType.Jpeg;
options.HorizontalResolution = 200;
options.VerticalResolution = 200;
// Take the image of your worksheet
SheetRender sr = new SheetRender(worksheet, options);
sr.ToImage(0, "e:\\test2\\out1.jpg");
Please find attached the output image after using the above sample code that has retained background color of the worksheet cells.
out1.jpg (279.6 KB)
One of team member from Aspose.Words team will review it and provide assistance soon. @alexey.noskov FYI.
1 Like
@RaghulBabu214,
In the meantime, you may refer to the reply in your other thread for your reference.
1 Like
can you help me with aspose ocr on top of
private static void InsertHtmlTable(DocumentBuilder builder, Workbook workbook, Worksheet worksheet, Range range, ReplacingArgs e, string r = “D8:G16”)
{
worksheet.PageSetup.BlackAndWhite = false;
worksheet.PageSetup.PrintArea = "B2:S411";
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = (Aspose.Cells.Drawing.ImageType)ImageType.Jpeg;
options.HorizontalResolution = 200;
options.VerticalResolution = 200;
SheetRender sr = new SheetRender(worksheet, options);
// Save the image to a temp file with .jpg extension
string tempImagePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".jpg");
sr.ToImage(0, tempImagePath);
Shape shape = builder.InsertImage(tempImagePath);
shape.WrapType = WrapType.Square;
shape.AllowOverlap = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Margin;
shape.HorizontalAlignment = HorizontalAlignment.Left;
shape.VerticalAlignment = VerticalAlignment.Top;
}
so that I can search in word … Basically what I need is when inserting an image in word I wanted itto be searchable…Is there a way to achieve this using apose ocr or any other if so kindly help me with code
with emf I am not able to…
Basically after saving through code whenever I open document and searh for something in image I need to land there
@RaghulBabu214,
You’ve already shared these queries and requirements in another thread. Kindly continue following up that thread for updates and replies. We appreciate your patience, and a member of the Aspose.Words team will assist you soon.
1 Like