1

I am a newbie and i want to generate PDF from Infragistics, Xamdatagrid. However as Infragistics doesnt provide this functionality ,i have generated XPS of Xamdatagrid and wants to convert that to XPS programitically. What are the possible work around and third party tool to do that?

Umair Noor
  • 442
  • 4
  • 17
  • 1
    It seems that your question is a [duplicate of this one](http://stackoverflow.com/questions/6395874/convert-xps-to-pdf-in-wpf-c-sharp-application) – Steve Aug 23 '12 at 09:20
  • 1
    @Steve yes but the answers provided there were not seem helpful. – Umair Noor Aug 23 '12 at 11:41
  • As an alternative, you could export to an Excel file as [demonstrated here](http://www.infragistics.com/products/wpf/sample/data-grid/export-to-excel), then it's easy to use Excel.Interop to create a pdf from the excel file. if you like the idea I could put the code needed for Excel.Interop as an answer. – Steve Aug 23 '12 at 12:34
  • Can you provide a (link to a) sample XPS that you generated? – Kurt Pfeifle Aug 23 '12 at 13:52
  • You can try to use online XPS to PDF Api at http://www.convertapi.com/xps-pdf-api – Tomas Aug 23 '12 at 14:00
  • @Steve i have successfully converted the xamdatagrid to excel,plz do tell me anywork around from excel to pdf – Umair Noor Aug 24 '12 at 10:31

3 Answers3

2

If you export the xamDataGrid in an excel file then is pretty simple to use Excel.Interop and ask excel to export its workbook in PDF format

// Export an excel workbok in PDF format with landscape orientation
private static void ExportWorkbookToPDF(string workbook, string output)
{
    Microsoft.Office.Interop.Excel.Application excelApplication = 
                     new Microsoft.Office.Interop.Excel.Application();
    excelApplication.ScreenUpdating = false;
    excelApplication.DisplayAlerts = false;
    excelApplication.Visible = false;

    Microsoft.Office.Interop.Excel.Workbook excelWorkbook = 
                     excelApplication.Workbooks.Open(workbook);

    if (excelWorkbook == null)
    {
        excelApplication.Quit();
        excelApplication = null;
        excelWorkbook = null;
        throw new NullReferenceException("Cannot create new excel workbook.");
    }
    try
    {
        ((Microsoft.Office.Interop.Excel._Worksheet)excelWorkbook.ActiveSheet).PageSetup.Orientation = 
                Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
        excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, output);
    }
    finally
    {
        excelWorkbook.Close();
        excelApplication.Quit();
        excelApplication = null;
        excelWorkbook = null;
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

if you wish to create only pdf. then the simplest thing have any pdf printer on machine. One like PDF Creater and then just call the printing thing like below on the XamDataGrid.

make sure you select the PDF Printer on the Printer Selection Dialog Box.

             // 1. Create Report object
            Report reportObj = new Report();

            // 2. Create EmbeddedVisualReportSection section. 
            // Put the grid you want to print as a parameter of section's constructor
            EmbeddedVisualReportSection section = new EmbeddedVisualReportSection(xamdg);

            // 3. Add created section to report's section collection
            reportObj.Sections.Add(section);

            // Optional. If you have progress indicator set its Report property to created report
           // progressInfo.Report = reportObj;

            // 4. Call print method
            reportObj.Print(true, false);
JSJ
  • 5,653
  • 3
  • 25
  • 32
0

You can also use thirdparty software like GhostXPS. http://www.ghostscript.com/download/gxpsdnld.html

Just start the convert process with the correct arguments and it will generate the PDF for you. The drawback is that you have to save the files temporary to the disk and check the return code. Also make sure you are not violating the GNU license

sam1589914
  • 806
  • 4
  • 9