We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Printing and fitting the spreadsheet to one page

Good day,

I am having trouble with printing from the spreadsheet control. I have replicated the code from the print sample application and I cant get either method to work quire right. The first method:

"PrintPreviewForm previewform = new PrintPreviewForm() { sfspreadsheet = this.spreadsheet1 };
previewform.show();"

this method works well except for the fact that it does not fit the spreadsheet data on a single page. For example, if I open a spreadsheet that has data from col. A to col. U, I need it to be resized to fit on a single page, but it spreads it out over 9 pages.

The second method:

"PrintFrompdfViewer( this.spreadsheet1);"

I am able to fit everything on one page with this method using settings.layoutoptions and it displays perfectly in the preview dialog, but when I actually print it it only prints a blank page for some reason.

Any assistance with this would be hugely appreciated.

thanks.

13 Replies

KB Kanimozhi Bharathi Syncfusion Team September 13, 2016 11:22 AM UTC

Hi Travis Chambers, 
 
Thank you for contacting Syncfusion Support. 
 
We have analyzed your query. You can print the spreadsheet data in a single page by setting the LayoutOptions as FitSheetOnOnePage. We have prepared a sample based on your requirement. Please find the link below, 
 
 
We have also checked by printing in XPS Viewer and One note book and it correctly sends only one page for print. Please find the attachment for your reference. 
 
 
If the printing issue still reproduces at your end, modify the above sample to reproduce the issue and update us with the replication procedure and product version details. So that we will analyze and update you with the appropriate solution. 
 
Regards 
Kanimozhi B 



TC Travis Chambers September 13, 2016 11:58 AM UTC

Thank you for your response. I have attempted to open the sample you provided, however I am getting an error for each form within the solution. 

For form 1 is says "Could not find type 'DemoCommon.Grid.GridDemoForm'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built using settings for your current platform or Any CPU."

For PrintPreviewForm is says "Could not find type 'Syncfusion.Windows.Forms.MetroForm'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built using settings for your current platform or Any CPU."

Thank you.


TC Travis Chambers September 13, 2016 12:18 PM UTC

Additionally, I need it to print in Landscape orientation.

Thanks.


KB Kanimozhi Bharathi Syncfusion Team September 14, 2016 04:58 AM UTC

Hi Travis Chambers, 
 
Sorry about the inconvenience. 
 
We have prepared the sample based on your requirement of printing in “Landscape” orientation by setting the PageSettings of PdfDocument’s Orientation as “LandScape” like below code example, 
 
//Create the previewdialog for print the document. 
PrintPreviewDialog printdialog = new PrintPreviewDialog(); 
 
//Create the pdfviewer for load the document. 
PdfViewerControl pdfviewer = new PdfViewerControl(); 
 
// PdfDocumentViewer 
MemoryStream pdfstream = new MemoryStream(); 
 
ExcelToPdfConverter converter = new ExcelToPdfConverter(spreadsheetControl.Workbook); 
//Intialize the PdfDocument 
PdfDocument pdfDoc = new PdfDocument(); 
 
//Intialize the ExcelToPdfConverter Settings 
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings(); 
settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; 
 
 
//Assign the PdfDocument to the templateDocument property of ExcelToPdfConverterSettings 
settings.TemplateDocument = pdfDoc; 
settings.DisplayGridLines = GridLinesDisplayStyle.Invisible; 
 
//Convert Excel Document into PDF document 
pdfDoc = converter.Convert(settings); 
 
//Setting the Orientation 
pdfDoc.PageSettings.Orientation = PdfPageOrientation.Landscape; 
 
//Save the PDF file 
pdfDoc.Save(pdfstream); 
 
//Load the document to pdfviewer 
pdfviewer.Load(pdfstream); 
 
printdialog.Document = pdfviewer.PrintDocument; 
 
printdialog.ShowDialog(); 
 
 
 
For more information regarding page settings, please refer the below UG: 
 
 
Regarding this issue “Could not find type 'Syncfusion.Windows.Forms.MetroForm”, can you please check whether “Syncfusion.Shared.Base.dll” assembly is correctly referenced in your sample and we have resolved this issue “"Could not find type 'DemoCommon.Grid.GridDemoForm'”. please find the updated sample below, 
 
 
 
Regards 
Kanimozhi B 



TC Travis Chambers September 14, 2016 12:19 PM UTC

Thank you again for your response. The sample does work now, however my issues still remain. The code to convert to landscape you provided is for the pdfviewer option (the "print" button). While this does display the document perfectly, when you select the print option within the pdfviewer it prints a blank page as if the document being viewed and the document being printed are two seperate docs with the print doc being a new blank one. Your sample does this too.

That being said, the other option (using the printpreviewform / "Print Preview" button) does print the document but that is where it is not printing in landscape format and the above code does not appear to apply to that method. So, I either need the pdfviewer option to actually print the document being viewed or the printpreviewform to print in landscape.

Thank you again for all of your help!


KB Kanimozhi Bharathi Syncfusion Team September 15, 2016 11:31 AM UTC

Hi Travis Chambers, 

We have checked the issue with our sample. But we were unable to reproduce the issue. So could you please share us the excel which you used for printing which will help us to analyze the issue better and provide you appropriate solution. 


Regards 
Kanimozhi B 



TC Travis Chambers October 18, 2016 04:05 PM UTC

Good day,

Please see the attached excel doc that I am using to print. To recap my issues:

Using this method:

  private void button2_Click(object sender, EventArgs e)
        {
            PrintPreviewForm previewform = new PrintPreviewForm() { sfspreadsheet = this.spreadsheet1 };
            previewform.Show();

 private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDialog dialog = new PrintDialog();

            int pageCount = pdfDocumentView1.PageCount;
            dialog.AllowPrintToFile = true;
            dialog.AllowSomePages = true;
            dialog.PrinterSettings.FromPage = 1;
            dialog.PrinterSettings.ToPage = pageCount;
            dialog.PrinterSettings.MaximumPage = pageCount;
            dialog.PrinterSettings.MinimumPage = 1;
            dialog.PrinterSettings.DefaultPageSettings.Landscape = true;
        
            

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                dialog.Document = pdfDocumentView1.PrintDocument;
                dialog.Document.Print();
            }
        }

     
This fits to one page and it prints just fine as well, however when it prints it does no do so in landscape but instead in tries to fit in all into the portrait view.

Using this secondmethod:

 private void button3_Click(object sender, EventArgs e)
        {
            PrintFromPdfViewer(this.spreadsheet1);
        }
        private static void PrintFromPdfViewer(Spreadsheet spreadsheetControl)
        {
            //Create the previewdialog for print the document. 
            PrintPreviewDialog printdialog = new PrintPreviewDialog();

            //Create the pdfviewer for load the document. 
            PdfViewerControl pdfviewer = new PdfViewerControl();

            // PdfDocumentViewer 
            MemoryStream pdfstream = new MemoryStream();

            ExcelToPdfConverter converter = new ExcelToPdfConverter(spreadsheetControl.Workbook);
            //Intialize the PdfDocument 
            PdfDocument pdfDoc = new PdfDocument();

            //Intialize the ExcelToPdfConverter Settings 
            ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
            settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;


            //Assign the PdfDocument to the templateDocument property of ExcelToPdfConverterSettings 
            settings.TemplateDocument = pdfDoc;
            settings.DisplayGridLines = GridLinesDisplayStyle.Invisible;

            //Convert Excel Document into PDF document 
           
            pdfDoc = converter.Convert(settings);

            //Setting the Orientation 
            pdfDoc.PageSettings.Orientation = PdfPageOrientation.Landscape;

            //Save the PDF file 
            pdfDoc.Save(pdfstream);

            //Load the document to pdfviewer 
            pdfviewer.Load(pdfstream);

            printdialog.Document = pdfviewer.PrintDocument;

            pdfDoc.Save("SaveTest");
            printdialog.ShowDialog();
        }

Here the document shows great in the printpreview box, however when I print it only prints a blank page.

I need to be able to print the doc successfully in landscape format.

Also, if it is possible to add in an option to choose to save as a pdf document without printing, that is a feature I sorely need as well.

Thank you for your assistance!

Attachment: sanitized_sample_13d2542f.zip


KB Kanimozhi Bharathi Syncfusion Team October 19, 2016 11:58 AM UTC

Hi Travis Chambers,  
   
We have checked your orientation issue. You can overcome this issue by setting the PageSettings of PdfDocument and PageSetup of Worksheet’s Orientation as “LandScape” while printing as given in the following code example.  
  
//Create the previewdialog for print the document.  
PrintPreviewDialog printdialog = new PrintPreviewDialog();  
  
//Create the pdfviewer for load the document.  
PdfViewerControl pdfviewer = new PdfViewerControl();  
  
// PdfDocumentViewer  
MemoryStream pdfstream = new MemoryStream();  
  
//Set Orientation in Worksheet also, 
spreadsheetControl.Workbook.ActiveSheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;  
 
ExcelToPdfConverter converter = new ExcelToPdfConverter(spreadsheetControl.Workbook);  
 
//Intialize the PdfDocument  
PdfDocument pdfDoc = new PdfDocument();  
  
//Intialize the ExcelToPdfConverter Settings  
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();  
settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;  
  
  
//Assign the PdfDocument to the templateDocument property of ExcelToPdfConverterSettings  
settings.TemplateDocument = pdfDoc;  
settings.DisplayGridLines = GridLinesDisplayStyle.Invisible;  
  
//Convert Excel Document into PDF document  
pdfDoc = converter.Convert(settings);  
  
//Setting the Orientation  
pdfDoc.PageSettings.Orientation = PdfPageOrientation.Landscape;  
  
//Save the PDF file  
pdfDoc.Save(pdfstream);  
  
//Load the document to pdfviewer  
pdfviewer.Load(pdfstream);  
  
printdialog.Document = pdfviewer.PrintDocument;  
  
printdialog.ShowDialog();  
  
  
  
We have also prepared a sample based on your requirement of including an option to save the content as PDF.  Please find the sample link below, 
 
 
We were unable to reproduce the issue of printing the blank pages in our side. For your reference, please find the attached PDF’s in LandScape and Potrait orientation which are generated from the sample. 
 
 
If the printing issue still persists, modify the above sample to reproduce the issue and send it to us with the replication procedure and product version details, so that we could check and provide an appropriate solution. 
  
Regards  
Kanimozhi B 


TC Travis Chambers October 19, 2016 05:23 PM UTC

Thank you so much! The save to pdf option works and then I can print from there, so I believe that pretty much solves my issue. 

I do have one last question regarding this issue however. When it saves to pdf it hows typical margins at the top and bottom of the page (rougly 1 inch margins). Is there a way to remove the margins to reduce the white space?

Thanks again!


KB Kanimozhi Bharathi Syncfusion Team October 20, 2016 12:11 PM UTC

Hi Travis Chambers,  
 
You can set the top and bottom margin of the pdf page like below code example, 
 
 
pdfDoc.PageSettings.Margins.Top = 0; 
pdfDoc.PageSettings.Margins.Bottom = 0; 
 
 
For more information, please refer the below UG link 
 
 
Regards 
Kanimozhi B 



TC Travis Chambers October 24, 2016 05:31 PM UTC

Thank you very much. Unfortunately that did not change the margins.  So, I am thinking that my issue may be with the spreadsheet's margins, not the pdf margins. I am comparing this document in pdf to the same excel document when saving it to pdf from Excel. However, in Excel I can modify the margins to "0" before saving to pdf. Does Spreadsheet have it's own margin settings that I may be missing?

Thanks!


KB Kanimozhi Bharathi Syncfusion Team October 26, 2016 03:54 AM UTC

Hi Travis Chambers, 
 
We were able to reproduce the margin issue with PDF and forwarded the issue internally to XlsIO team. Also we have a created  incident in your account. So can you please log into our support website for further update on this issue. 
 
Regards 
Kanimozhi B 



KB Kanimozhi Bharathi Syncfusion Team October 26, 2016 04:07 AM UTC

Hi Travis Chambers, 

We were able to reproduce the margin issue while exporting to PDF and forwarded the issue internally to XlsIO team. A support incident has been created under your account to track the status of this issue. Please log on to our support website to check for further updates. 

Regards 
Kanimozhi B 


Loader.
Live Chat Icon For mobile
Up arrow icon