Convert Xls/Xlsx (Special Images and Text) or Multiple Excel Files to PDF

It is really easy to convert xls/xlsx to PDF when there is only common data in Excel. How about converting xls/xlsx with special shapes or images to PDF? Sometimes, we also need to convert multiple excel files to one PDF. Now let use see how to convert special images in excel to be PDF first.

Convert One Xls/Xlsx of Special Images and Text to PDF

Not long ago.I got an Excel file and want to convert it to PDF. When I search in google, I found there are many converters, so I pick one to give it a try. When I opened the target PDF converted by xls, I felt really disappointed. You can see my xls file as below picture:

After I convert xls to PDF by the conversion tool, some texts are missing since it automatically enlarges the font. Also, the color of the data is changed. You can compare the two images.

Then, I hope to convert my excel file to PDF by other tools and expect the miracle happens. However, the fact proved that it was a waste of my time. Finally I have to give up trying. And regret not using this MS Excel component. I had thought that I could convert my xls to PDF by other tools even my xls has special images and texts. Now, I have to use my commonly used library Spire.XLS for .NET. The target PDF file is really better, clear, no data lost. I think these would be the highest requirements for the conversion task. It did not let me down at least. Now you can see the target PDF that I convert by this .net component.

When convert xls/xlsx to PDF by this excel component, after loading your excel file to system, you need to set a PDF template including orientation, width, height, margin, rotate etc. Then, using the class Spire.Xls.Converter.PdfConverter to convert Excel to PDF using the template set before. Finally save the PDF file by the method: SaveToFile(string filename). You can view the whole code here:      

Detail Code for Converting Xls/Xlsx to PDF

C# :

using Spire.Xls;
using Spire.Pdf;
using Spire.Xls.Converter;
namespace xls_to_pdf
{
    class Program
    {
        static void Main(string[] args)
        {    
              // load Excel file
              Workbook workbook = new Workbook();
              workbook.LoadFromFile(@"D:\michelle\my file\xlstopdf.xls");
              // Set PDF template
              PdfDocument pdfDocument = new PdfDocument();
              pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
              pdfDocument.PageSettings.Width = 970;
              pdfDocument.PageSettings.Height = 850;
              //Convert Excel to PDF using the template above
              PdfConverter pdfConverter = new PdfConverter(workbook);
              PdfConverterSettings settings = new PdfConverterSettings();
              settings.TemplateDocument = pdfDocument;
              pdfDocument = pdfConverter.Convert(settings);
              // Save and preview PDF
              pdfDocument.SaveToFile("sample.pdf");
              System.Diagnostics.Process.Start("sample.pdf");
          }
      }
  }

Please make sure that you add Spire.Common.dll, Spire.License.dll, Spire.Pdf.dll, Spire.XLS.dll as references in the Bin folder from your download Spire.XLS, we do not need to use other PDF library since Spire.XLS has alrady contain them both.  If you think the solution used above is effective, the target PDF file is what you expect, and want to try. You can download .NET Excel component introduced above. Now, let us see how to convert multiple Excel files to PDF.

Convert Multiple Excel Files to One PDF

Here I will introduce two solutions to convert multiple excel files to One PDF file. The first one is to merge all Excel files together into one and then, convert this xls/xlsx to PDF. The other is to convert each excel files to be PDF file and then, merge all the PDF files into one PDF. As for the first solution, we only need Spire.XLS for .NET and do not need any other PDF library since in the downloaded Spire.XLS Bin folder, there are already Spire.Pdf library. We only need to add the dll in our project, while for the second solution, even Spire.XLS itself can convert excel to PDF file, it can not merge PDF files into one. So if you choose the second solution, certainly you need another PDF library. All in one, if you use solution one, you only need Spire.XLS. While, if you choose the second solution, apart from Spire.XLS, you still need another PDF library. Now, let use see the first solution.

First Solution: Merge Exel files to One Excel Sheet and Convert to PDF

When merge Excel files into one excel sheet, first we need to load excel files from system. Then, call the copy each excel data to sheet by calling method provided by Spire.XLS : Spire.Xls.Worksheet.Copy(CellRange sourceRange, Worksheet worksheet, int destRow, int destColumn, bool copyStyle); Then, convert the excel file to PDF by the solution I introduced above. In my solution, I also set the background color of the excel file data.  Here you can see all the details:

C#:

       Workbook dest = new Workbook();
            Worksheet result = dest.CreateEmptySheet("copied sheet");
            //load the first workbook
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"D:\michelle\my file\page break.xlsx", ExcelVersion.Version2010);
            //load the second workbook
            Workbook workbook2 = new Workbook();
            workbook2.LoadFromFile(@"D:\michelle\my file\merge excel.xlsx", ExcelVersion.Version2010);
            //load the third workbook
            Workbook workbook3 = new Workbook();
            workbook3.LoadFromFile(@"D:\michelle\my file\Backgroundcolor.xlsx", ExcelVersion.Version2010);
            //merge three excel files into one excel sheet
            Worksheet sheetDest = workbook.Worksheets[0];
            CellRange sourceRange=workbook2.Worksheets[0].AllocatedRange;
            CellRange souce2 = workbook3.Worksheets[0].AllocatedRange;
            sheetDest.Copy(sourceRange, sheetDest, sheetDest.AllocatedRange.RowCount+2, 1,true);
            sheetDest.Copy(souce2, sheetDest, sheetDest.AllocatedRange.RowCount+2, 1, true);
            //set the backgroundcolor of Range["A19:D19"]
            Worksheet result1 = workbook.Worksheets[0];
            result1.Range["A20:D20"].Style.Color = Color.Yellow;
            result1.Range["A21:D38"].Style.Color = Color.SpringGreen;
            result1.Range["A40:D40"].Style.Color = Color.Red;
            result1.Range["A41:D55"].Style.Color = Color.Orange;
            //convert excel to pdf
            //set PDF template
            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
            pdfDocument.PageSettings.Width = 970;
            pdfDocument.PageSettings.Height = 850;
            //Convert Excel to PDF using the template above
            PdfConverter pdfConverter = new PdfConverter(workbook);
            PdfConverterSettings settings = new PdfConverterSettings();
            settings.TemplateDocument = pdfDocument;
            pdfDocument = pdfConverter.Convert(settings);
            // Save and preview PDF
            pdfDocument.SaveToFile("sample.pdf");
            System.Diagnostics.Process.Start("sample.pdf");

See the effective screenhot here:


Second Solution: Convert Each Excel File to PDF and Merge All PDF Files into One

As convert excel file to PDF, I have refered. Now suppose I have convert three excel files to three PDF files, I need to merge all the PDF files together. I need to add this PDF library  https://pdfapi.codeplex.com in my project. After loading each PDF file from system by this string array: Spire.Pdf.PdfDocument array, we need to import the second and third PDF file data into the first PDF file. We need to call the methods: public void AppendPage(PdfDocument doc) and public PdfPageBase InsertPage(PdfDocument doc, int pageIndex). Now let us see the details below:

C#:

private void button1_Click(object sender, EventArgs e)
         {
             //pdf document list
             String[] files = new String[]
             {
                 @"..\PDFmerge0.pdf",
                 @"..\ PDFmerge1.pdf",
                 @"..\ PDFmerge2.pdf"
             };
             //open pdf documents            
             PdfDocument[] docs = new PdfDocument[files.Length];
             for (int i = 0; i < files.Length; i++)
             {
                 docs[i] = new PdfDocument(files[i]);
             }
             //append document
             docs[0].AppendPage(docs[1]);
            //import PDF pages
             for (int i = 0; i < docs[2].Pages.Count; i = i + 2)
             {
                 docs[0].InsertPage(docs[2], i);
             }

Related Article:

Convert Doc/Docx to PDF

Leave a Reply