سؤال

I have a project where I create sample documents. Code here :

 private void btnExcel_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        if (AutomationFactory.IsAvailable)
        {
            dynamic excel = AutomationFactory.CreateObject("Excel.Application");
            excel.Visible = true;

            dynamic workbook = excel.workbooks;
            workbook.Add();

            dynamic sheet = excel.ActiveSheet;
            dynamic cell = null;
            int i;
            int z = 20;

            for (i = 1; i <= 20; i++)
            {
                cell = sheet.Cells[i, 1];
                cell.Value = i.ToString();

                cell = sheet.Cells[i, 2];
                cell.Value = z.ToString();

                z--;
            }

        }

    }

As you can see, this is for Excel documents. Is there any way I can something like this and export in to PDF files? Thanks for the insights.

هل كانت مفيدة؟

المحلول

Yes you can.

You can call a WCF service operation passing it all the data that it needs. Then do all what is required to generate the PDF file. After you've done that, you have two options:

  • You can send back the bytes as the result of the operation. In this case, you can show the user a SaveDialog to save the file to his local disk.
  • You can send the generated file to another aspx page and display the PDF file to the user who then has the choice to save it to his local disk or simply view it.

I hope that was what you're looking for.

نصائح أخرى

Sure, you can. But why?! As I understand with AutomationFactory.CreateObject you can do it when on client machine Excel or PDF are installed.

But dealing with web application (especially when it's not an enterprise applications) you want to see pdf/xls/xlsx/... anywhere and you can relay on what is installed on client machine or not.

I have similar situation for one of my projects. We are using XPS format to show in silverlight because it has native support. And other formats are converted to xps.

There nuances in conversion, e.g. for excel documents I think best way is to convert each sheet separately, and implement special kind viewer that differs from viewer, for example, for word documents or pdf's.

There are 3rd party viewers that allow to view not only xps on silverlight but pdf too. But without them you can only let user to download PDF file not to view that, because these 3rd part viewer most likely convert pdf to xps under the hood.

Any way, especially without 3rd parties, it will take huge efforts to implement PDF viewer on silverlight. So I suggest you to use xps for viewing. But when you need to have pdf files for downloads than use XPS for viewing and PDF on download. In such case your converters will produce 2 formats for each sitation.

For example, look at

http://firstfloorsoftware.com/blog/announcement-document-toolkit-for-silverlight/

http://silverpdf.codeplex.com/

http://www.componentone.com/SuperProducts/PdfViewerSilverlight/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top