We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Display the ODS on Print Preview on opening

I want to achieve the state of PrintPreview on opening a ODS file. (Exactly as you press on M.Excel / LibreOffice/ OpenOffice Calc on ribbon File->Print). I’ve researched on your forums and found 2 possible solutions but none of them seems to work at the moment:

  1. Workbook workbook = new Workbook(sourceDir + “Book1.xlsx”);
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
    WorkbookPrintingPreview preview = new WorkbookPrintingPreview(workbook, imgOptions);

SheetPrintingPreview preview2 = new SheetPrintingPreview(workbook.Worksheets[0], imgOptions);
// BUT don’t know how/where to use the instances preview or preview2

  1. would be using SheetRender or WorkbookRender instances but then I have to select/use the printerName and I don’t need\want to specify the printer

    I saw a very simple and useful solution on Aspose Words at: https://docs.aspose.com/words/net/display-documents-in-print-preview/ but nothing similar on Aspose.Cells. For sure you have a simple solution for this easy task

@Remus87,

See the document with examples for your reference for your requirements:

@Remus87
Please also try the following code:

Workbook wb = new Workbook(sourceDir + "Book1.xlsx");
WorkbookRender wr = new WorkbookRender(wb, new ImageOrPrintOptions());

TestPrintDocument document = new TestPrintDocument(wr);

PrintPreviewDialog previewDlg = new PrintPreviewDialog();

// Pass the print document to the Print Preview dialog.
previewDlg.Document = document;

// Specify additional parameters of the Print Preview dialog.
previewDlg.ShowInTaskbar = true;
previewDlg.MinimizeBox = true;
previewDlg.PrintPreviewControl.Zoom = 1;
previewDlg.Document.DocumentName = "TestName.xlsx";
previewDlg.WindowState = FormWindowState.Maximized;

// Show the appropriately configured Print Preview dialog.
previewDlg.ShowDialog();

...

//TestPrintDocument definition
public class TestPrintDocument : PrintDocument
{
	private WorkbookRender mWr;
	public TestPrintDocument(WorkbookRender wr)
	{
		mWr = wr;
	}

	protected override void OnPrintPage(PrintPageEventArgs e)
	{
		base.OnPrintPage(e);

		int current = mWr.CustomPrint(true, e);
		if (current < mWr.PageCount)
		{
			e.HasMorePages = true;
		}
	}
}

Hi!
Thanks #Peyton.Xu for your effort. This works fine but it calls the windows built-in printer form. I just need the print preview that is displayed by the default text editor. I attach a picture below of where I want to reach because maybe I didn’t explained clearly.
#Amjad_Sahi I tried that example but as explained in the tread it won’t work because it asks to specify the printer name and I don’t want that. Is just exactly as you open any spreadsheet when you go to print the spreadsheet editor will add by default the printer.
Thanks in advanceSample.PNG (76.2 KB)

Does it not use default printer as in the case of rendering MS Word document (reference link)?

Yes on the code snippet that Peyton.Xu supplied it calls the default printer but is coming in a .NET built in form as shown in the below picture and not in the spreadsheet editor application as shown in the previous message attachmentSample2.PNG (6.2 KB)

@Remus87,

Thanks for sharing further details.
We will evaluate it and get back to you soon.

@Remus87,

We evaluated your issue further.

  1. We first tried to create similar MS Word VSTO add-ins (as per the document), added it to Microsoft Word, then opened a docx file, it can directly go to printview GUI.
  2. Then we created a similar VSTO add-ins for Microsoft Excel, but it does not work as MS Word VSTO add-ins. Please find attached the VSTO project and screenshots for your reference.
    Printview Workbooks.zip (63.6 KB)
    image_2021_01_07T08_37_49_401Z.png (41.5 KB)
    image_2021_01_07T08_39_07_790Z.png (32.2 KB)

Furthermore, we have also tried using Aspose.Words API only in a project, the ActivePrintPreviewDialog is not defined. But we don’t think, using Aspose.Words can get the print view in Microsoft Word application. Please create a sample console application (runnable) as a working sample using Aspose.Words API to accomplish the task as per your needs. After getting the working sample of Aspose.Words API, we will try to mimic the same using Aspose.Cells APIs.

We are developing only spreadsheets at the moment using Aspose Cells. We plan to develop ODT in the next half of the year and then we plan to purchase licenses for Aspose Words. The current task is for ODS developed in Cells and not at all Words. I was just providing the reference with Words as a simple scenario to showcase that is possible to do that in the Aspose architecture. I’ve created a simple project using Interop Excel package to illustrate exactly what what was the purpose of the tread. (Is just a line of code really)Test_PrintPreview.zip (23.4 KB)

@Remus87,

Thanks for the VSTO project.

But we requested a sample project to accomplish the task via Aspose.Words APIs because we did test it already via Aspose.Words and found the example mentioned in your referred document is not working in your desired way. The reason is as we think your desired task (open the office files in print view of GUI in MS Excel/Word) cannot be performed without using a call to VSTO (Visual Studio Tools for Office) or office interop. APIs. You know Aspose APIs are independent and are written in managed .NET. The Aspose APIs do not depend or use MS Office (interop.) or VSTO or any other relevant APIs.

It’s ok then . Thanks for your help. I should stick with that .NET form supplied by #Peyton.Xu and see if I can customise it (make the Page Setup menu to display on it as at the moment doesn’t have any options other than Print button and few View options)

@Remus87,

Yes, you may use the sample code suggested by @Peyton.Xu. In the event other queries or issue, feel free to write us back.