728x90
반응형

Syncfusion 을 이용해 액셀파일의 내용을 PDF 파일로 변환하는 코드입니다.

 

        private void ConvertSyncfusion()

        {

            using (Syncfusion.XlsIO.ExcelEngine excelEngine = new Syncfusion.XlsIO.ExcelEngine())

            {

                Syncfusion.XlsIO.IApplication application = excelEngine.Excel;

                application.DefaultVersion = Syncfusion.XlsIO.ExcelVersion.Excel2013;

                Syncfusion.XlsIO.IWorkbook workbook = application.Workbooks.Open(this.pathTextBox.Text, Syncfusion.XlsIO.ExcelOpenType.Automatic);

 

                // pdf 출력시 옵션 정의

                Syncfusion.ExcelToPdfConverter.ExcelToPdfConverterSettings settings = new Syncfusion.ExcelToPdfConverter.ExcelToPdfConverterSettings();

                settings.LayoutOptions = Syncfusion.ExcelToPdfConverter.LayoutOptions.FitSheetOnOnePage;

                settings.DisplayGridLines = Syncfusion.ExcelToPdfConverter.GridLinesDisplayStyle.Invisible;

 

                // 변환할 액셀 파일의 상위 경로

                string pdfFilePath = Directory.GetParent(this.pathTextBox.Text).FullName;

 

                // 시트별로 각 시트명으로 pdf 출력하는 경우

                if (this.sheetCheckBox.Checked)

                {

                    foreach (Syncfusion.XlsIO.IWorksheet sheet in workbook.Worksheets)

                    {

                        Syncfusion.ExcelToPdfConverter.ExcelToPdfConverter converter = new Syncfusion.ExcelToPdfConverter.ExcelToPdfConverter(sheet);

 

                        Syncfusion.Pdf.PdfDocument pdfDocument = new Syncfusion.Pdf.PdfDocument();

                        pdfDocument = converter.Convert(settings);

                        pdfDocument.Save(Path.Combine(pdfFilePath, sheet.CodeName + ".pdf"));

                    }

                }

                // 전체를 pdf 출력하는 경우

                else

                {

                    Syncfusion.ExcelToPdfConverter.ExcelToPdfConverter converter = new Syncfusion.ExcelToPdfConverter.ExcelToPdfConverter(workbook);

                    Syncfusion.Pdf.PdfDocument pdfDocument = new Syncfusion.Pdf.PdfDocument();

                    pdfDocument = converter.Convert(settings);

 

                    string pdfFileName = System.IO.Path.ChangeExtension(this.pathTextBox.Text, ".pdf");

 

                    pdfDocument.Save(pdfFileName);

 

                    // pdf 파일 오픈

                    if (this.openFileCheckBox.Checked)

                    {

                        System.Diagnostics.Process.Start(pdfFileName);

                    }

                }

            }

        }

 

728x90
반응형

'Syncfusion' 카테고리의 다른 글

[Syncfusion] Excel(sheet) to Pdf  (0) 2020.06.27

+ Recent posts