C ා merge multiple formats into PDF

Document merging is an efficient way of document processing. If there is a way to combine many different types of documents into one document format, it will provide us great convenience in document storage management. So this article shows you how to use a free component Free Spire.Office for .NET To realize the method of merging various kinds of documents into PDF. The document formats that can be merged in this method can be doc, docx, xls, xlsx, PDF, etc. It will be described in detail below.

Usage: after installing free spire.office for. Net, add three files: spire.doc.dll, spire.xls.dll and spire.pdf.dll, as shown below:

After adding, add the following using directive to the namespace:

using Spire.Doc;
using Spire.Xls;
using Spire.Pdf;
using System.IO;

Before merging documents:

Main code:

           //statement PdfDocument object array
            PdfDocument[] documents = new PdfDocument[4];
            using (MemoryStream ms1 = new MemoryStream())
            {
                //Load doc File
                Document doc = new Document(@"C:\Users\Administrator\Desktop\sample.doc", Spire.Doc.FileFormat.Doc);
                //Transformation PDF,And save it as a stream
                doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
                //Load PDF The stream file is PdfDocument The first element of an array of objects
                documents[0] = new PdfDocument(ms1);
            }
            using (MemoryStream ms2 = new MemoryStream())
            {
                //Load docx File 
                Document docx = new Document(@"C:\Users\Administrator\Desktop\test.docx", Spire.Doc.FileFormat.Docx2010);

                //Transformation PDF,And save it as a stream 
                docx.SaveToStream(ms2, Spire.Doc.FileFormat.PDF);
                //Load PDF The stream file is PdfDocument The second element of an array of objects
                documents[1] = new PdfDocument(ms2);
            }

            using (MemoryStream ms3 = new MemoryStream())
            {
                //Load xlsx File
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx", ExcelVersion.Version97to2003);
                //Transformation PDF,And save it as a stream
                workbook.SaveToStream(ms3, Spire.Xls.FileFormat.PDF);
                //Load PDF The stream file is PdfDocument The third element of an array of objects
                documents[2] = new PdfDocument(ms3);
            }

            //Load pdf Documents, as PdfDocument The fourth element of an array of objects
            documents[3] = new PdfDocument(@"C:\Users\Administrator\Desktop\sample.pdf");
            //Merge the first three documents into the fourth and save as a new PDF File
            for (int i = 2; i > -1; i--)
            {
                documents[3].AppendPage(documents[i]);
            }

            documents[3].SaveToFile("Result.pdf");   

After the code is completed, debug and run the project to generate files.

Combination effect display:

All of the above is a method to merge different format files into PDF files. If you want to merge multiple single format PDF files into one file, please refer to this article: http://www.cnblogs.com/Yesi/p/5604166.html .

If you like this article, please reprint it

Thanks for browsing!

Tags: C#

Posted on Mon, 04 May 2020 09:09:44 -0400 by umbra