Merge and Split |
The PDF Creator offers an API for merging PDF documents or extracting pages from a PDF document using MergePdf and SplitPdf classes.
The code sample below is taken from MergeSplitDemo sample application:
protected void btnCreatePDF_Click(object sender, EventArgs e) { string workingFolderPath = System.IO.Path.Combine(Server.MapPath("~"), "Files"); string firstFilePath = System.IO.Path.Combine(workingFolderPath, "oss.pdf"); string secondFilePath = System.IO.Path.Combine(workingFolderPath, "google.pdf"); string mergedFilePath = System.IO.Path.Combine(workingFolderPath, "MergeFileResult.pdf"); string extractedFilePath = System.IO.Path.Combine(workingFolderPath, "SplitFileResult.pdf"); // merge files into disk file string[] filesToMerge = new string[2]; filesToMerge[0] = firstFilePath; filesToMerge[1] = secondFilePath; // merge the two files into mergedFilePath on disk MergePdf.Merge(filesToMerge, mergedFilePath); // extract first page from the merged document and create a PDF file with it SplitPdf.ExtractPages(mergedFilePath, 0, 1, extractedFilePath); //merge streams System.IO.FileStream fs1 = new System.IO.FileStream(firstFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream fs2 = new System.IO.FileStream(secondFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.Stream[] streamsToMerge = new System.IO.Stream[2]; streamsToMerge[0] = fs1; streamsToMerge[1] = fs2; // prepare Response Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=MergeStreamsResult.pdf"); Response.AddHeader("content-type", "application/pdf" ); //Save the merge result directly on the Response stream to browser MergePdf.Merge(streamsToMerge, Response.OutputStream); //send buffered output to client browser Response.End(); // close the source streams fs1.Close(); fs2.Close(); }