/// <summary> /// Copy a document to other document. Supported any formats (PDF, DOCX, RTF, HTML). /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/importing-element-net-csharp-vb.php /// </remarks> static void ImportingElement() { string file1 = @"..\..\digitalsignature.docx"; string file2 = @"..\..\Parsing.docx"; string resultFile = "Importing.docx"; // Load files. DocumentCore dc = DocumentCore.Load(file1); DocumentCore dc1 = DocumentCore.Load(file2); // New Import Session to improve performance. var session = new ImportSession(dc1, dc); // Import all sections from source document. foreach (Section sourceSection in dc1.Sections) { Section destinationSection = dc.Import(sourceSection, true, session); dc.Sections.Add(destinationSection); } // Save the result. dc.Save(resultFile); // Show the result. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultFile) { UseShellExecute = true }); }
/// <summary> /// Import an Element with Styles from another document. Mode: KeepDifferentStyles. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-keep-different-styles.php /// </remarks> private static void ImportKeepDifferentStyles() { // Mode: KeepDifferentStyles // The most useful mode to preserve formatting during importing. // 'KeepDifferentStyles' means to only copy styles that are different by formatting. // If the destination document already contains a style with the same name, // therefore an unique style name will be generated. // For example, a destination document contains a style "MyGreen" (FontSize = 20, Green, Underline). // And a source document also contains a style with name "Green" (FontSize = 20, Green, Underline). // Because of the formatting of styles are equal, the "Green" style will not be imported. // All imported elements linked to style "Green" will be remapped to style "MyGreen" DocumentCore source = DocumentCore.Load(@"..\..\SourceStyles.docx"); DocumentCore dest = new DocumentCore(); // Create a new style "MyGreen" (FontSize = 20, Green, Underline). CharacterStyle chStyle = new CharacterStyle("MyGreen"); chStyle.CharacterFormat.FontColor = Color.Green; chStyle.CharacterFormat.Size = 20; chStyle.CharacterFormat.FontName = "Calibri"; chStyle.CharacterFormat.UnderlineStyle = UnderlineType.Single; dest.Styles.Add(chStyle); dest.Content.End.Insert(new Run(dest, "This text has the style MyGreen.", new CharacterFormat() { Style = chStyle }).Content); // Create an ImportSession with mode 'KeepDifferentStyles'. ImportSession session = new ImportSession(source, dest, StyleImportingMode.KeepDifferentStyles); // Let's import a paragraph. // The imported paragraph contains a text with style "Green" (FontSize = 20, Green, Underline). // The style "Green" will not be imported, because we already have "MyGreen" with the same formatting. // All links to "Green" will be remapped to "MyGreen". Paragraph importedPar = dest.Import <Paragraph>((Paragraph)source.Sections[0].Blocks[0], true, session); dest.Content.End.Insert(importedPar.Content); // Save the destination document into DOCX format. string docPath = "KeepDifferentStyles.docx"; dest.Save(docPath); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true }); }
/// <summary> /// Import an Element with Styles from another document. Mode: UseDestinationStyles. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-use-destination-styles.php /// </remarks> private static void ImportUseDestinationStyles() { // Mode: UseDestinationStyles. // 'UseDestinationStyles' means to copy only styles wchich are don't exist // in the destination document. // If the destination document already contains a style with the same name, // therefore a style from a source will not be copied. // For example, a destination document contains a style "Green" (FontSize = 24, DarkGreen). // And a source document also contains a style with name "Green" (FontSize = 20, Green, Underline). // After the importing, the imported content will change its formatting correspondly to the "Green" style in the destination document. // Because style "Green" (FontSize = 20, Green, Underline) was not imported. DocumentCore source = DocumentCore.Load(@"..\..\SourceStyles.docx"); DocumentCore dest = new DocumentCore(); // Before importing a style from another document, let's create a style // with the same name but different formatting to see // how the name conflict will be resolved in mode 'UseDestinationStyles'. CharacterStyle chStyle = new CharacterStyle("Green"); chStyle.CharacterFormat.FontColor = Color.DarkGreen; chStyle.CharacterFormat.Size = 24; dest.Styles.Add(chStyle); dest.Content.End.Insert(new Run(dest, "First ", new CharacterFormat() { Style = chStyle }).Content); // Create an ImportSession with mode 'UseDestinationStyles'. ImportSession session = new ImportSession(source, dest, StyleImportingMode.UseDestinationStyles); // Let's import a 1st paragraph from the source document. // The paragraph contains a text marked by style "Green". // As a style with the same name is already exist, the new "Green" style will not be imported. Paragraph importedPar = dest.Import <Paragraph>((Paragraph)source.Sections[0].Blocks[0], true, session); dest.Content.End.Insert(importedPar.Content); // Save the destination document into DOCX format. string docPath = "UseDestinationStyles.docx"; dest.Save(docPath); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true }); }
/// <summary> /// Import an Element with Styles from another document. Mode: KeepSourceFormatting. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-keep-source-formatting.php /// </remarks> private static void ImportKeepSourceFormatting() { // Mode: KeepSourceFormatting. // 'KeepSourceFormatting' means to copy all required styles to the destination document, // generate unique style names if needed. // For example, a destination document contains a style "Green" (Calibri, FontSize = 20, Green, Underline). // And a source document also contains an equal style with the same name "Green" (Calibri, FontSize = 20, Green, Underline). // The style "Green" will be imported and renamed to "Green1". // All imported elements linked to style "Green" will be remapped to style "Green1". DocumentCore source = DocumentCore.Load(@"..\..\SourceStyles.docx"); DocumentCore dest = new DocumentCore(); // Let's create a style "Green" (Calibri, FontSize = 20, Green, Underline). CharacterStyle chStyle = new CharacterStyle("Green"); chStyle.CharacterFormat.FontName = "Calibri"; chStyle.CharacterFormat.FontColor = Color.Green; chStyle.CharacterFormat.Size = 20; chStyle.CharacterFormat.UnderlineStyle = UnderlineType.Single; dest.Styles.Add(chStyle); dest.Content.End.Insert(new Run(dest, "This text has the style Green.", new CharacterFormat() { Style = chStyle }).Content); // Create an ImportSession with mode 'KeepSourceFormatting'. ImportSession session = new ImportSession(source, dest, StyleImportingMode.KeepSourceFormatting); // Let's import a paragraph. // The imported paragraph contains a text with style "Green" (FontSize = 20, Green, Underline). // The style "Green" will be imported and renamed to "Green1", because we already have "Green". // All links in imported paragraph will be remapped to the style "Green1". Paragraph importedPar = dest.Import <Paragraph>((Paragraph)source.Sections[0].Blocks[0], true, session); dest.Content.End.Insert(importedPar.Content); // Save the destination document into DOCX format. string docPath = "KeepSourceFormatting.docx"; dest.Save(docPath); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true }); }
/// <summary> /// How to merge two documents: DOCX and PDF. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/split-and-merge-content-net-csharp-vb.php /// </remarks> public static void MergeTwoDocuments() { // Path to our combined document. string singleFilePath = "Single.docx"; string[] supportedFiles = new string[] { @"..\..\example.docx", @"..\..\example.pdf" }; // Create single document. DocumentCore dcSingle = new DocumentCore(); foreach (string file in supportedFiles) { DocumentCore dc = DocumentCore.Load(file); Console.WriteLine("Adding: {0}...", Path.GetFileName(file)); // Create import session. ImportSession session = new ImportSession(dc, dcSingle, StyleImportingMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section sourceSection in dc.Sections) { // Because we are copying a section from one document to another, // it is required to import the Section into the destination document. // This adjusts any document-specific references to styles, bookmarks, etc. // // Importing a element creates a copy of the original element, but the copy // is ready to be inserted into the destination document. Section importedSection = dcSingle.Import <Section>(sourceSection, true, session); // First section start from new page. if (dc.Sections.IndexOf(sourceSection) == 0) { importedSection.PageSetup.SectionStart = SectionStart.NewPage; } // Now the new section can be appended to the destination document. dcSingle.Sections.Add(importedSection); } } // Save single document to a file. dcSingle.Save(singleFilePath); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singleFilePath) { UseShellExecute = true }); }
/// <summary> /// This sample shows how to merge multiple DOCX, RTF, PDF and Text files. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/merge-multiple-files-net-csharp-vb.php /// </remarks> public static void MergeMultipleDocuments() { // Path to our combined document. string singlePDFPath = "Single.pdf"; string workingDir = @"..\..\"; List <string> supportedFiles = new List <string>(); // Fill the collection 'supportedFiles' by *.docx, *.pdf and *.txt. foreach (string file in Directory.GetFiles(workingDir, "*.*")) { string ext = Path.GetExtension(file).ToLower(); if (ext == ".docx" || ext == ".pdf" || ext == ".txt") { supportedFiles.Add(file); } } // Create single pdf. DocumentCore singlePDF = new DocumentCore(); foreach (string file in supportedFiles) { DocumentCore dc = DocumentCore.Load(file); Console.WriteLine("Adding: {0}...", Path.GetFileName(file)); // Create import session. ImportSession session = new ImportSession(dc, singlePDF, StyleImportingMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section sourceSection in dc.Sections) { // Because we are copying a section from one document to another, // it is required to import the Section into the destination document. // This adjusts any document-specific references to styles, bookmarks, etc. // // Importing a element creates a copy of the original element, but the copy // is ready to be inserted into the destination document. Section importedSection = singlePDF.Import <Section>(sourceSection, true, session); // First section start from new page. if (dc.Sections.IndexOf(sourceSection) == 0) { importedSection.PageSetup.SectionStart = SectionStart.NewPage; } // Now the new section can be appended to the destination document. singlePDF.Sections.Add(importedSection); } } // Save single PDF to a file. singlePDF.Save(singlePDFPath); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singlePDFPath) { UseShellExecute = true }); }
private void button1_Click(object sender, EventArgs e) { // //try1 // //Creates a PDF document // Spire.Pdf.PdfDocument finalDoc = new Spire.Pdf.PdfDocument(); // //Creates a string array of source files to be merged // string[] source = { textBox1.Text, textBox2.Text, textBox3.Text }; // //Merge PDF documents // Spire.Pdf.PdfDocumentBase.Merge(finalDoc, source); // //Save the document // finalDoc.SaveToFile("Sample.pdf"); // //try2 //Spire.Pdf.PdfDocument[] documents = new Spire.Pdf.PdfDocument[4]; //using (MemoryStream ms1 = new MemoryStream()) //{ // Document doc = new Document("c://users//dell//desktop//input//second.docx", Spire.Doc.FileFormat.Auto); // doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF); // documents[0] = new Spire.Pdf.PdfDocument(ms1); //} //using (MemoryStream ms2 = new MemoryStream()) //{ // Document jpg = new Document("c://users//dell//desktop//input//first.jpg", Spire.Doc.FileFormat.Auto); // jpg.SaveToStream(ms2, Spire.Doc.FileFormat.PDF); // documents[1] = new Spire.Pdf.PdfDocument(ms2); //} //using (MemoryStream ms3 = new MemoryStream()) //{ // Document pdf = new Document("c://users//dell//desktop//input//third.pdf", Spire.Doc.FileFormat.Auto); // pdf.SaveToStream(ms3, Spire.Doc.FileFormat.PDF); // documents[2] = new Spire.Pdf.PdfDocument(ms3); //} //documents[3] = new Spire.Pdf.PdfDocument("fourth.pdf"); //for (int i = 2; i > -1; i--) //{ // documents[3].AppendPage(documents[i]); //} //documents[3].SaveToFile("outputproblemstatement2.pdf"); //try3 // Path to our combined document. string singlePDFPath = "Single.pdf"; string workingDir = Path.GetFullPath(@"C:\Users\dell\Desktop\Input"); List <string> supportedFiles = new List <string>(); foreach (string file in Directory.GetFiles(workingDir, "*.*")) { string ext = Path.GetExtension(file).ToLower(); if (ext == ".docx" || ext == ".pdf" || ext == ".txt") { supportedFiles.Add(file); } } // Create single pdf. DocumentCore singlePDF = new DocumentCore(); foreach (string file in supportedFiles) { DocumentCore dc = DocumentCore.Load(file); Console.WriteLine("Adding: {0}...", Path.GetFileName(file)); // Create import session. ImportSession session = new ImportSession(dc, singlePDF, StyleImportingMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (SautinSoft.Document.Section sourceSection in dc.Sections) { SautinSoft.Document.Section importedSection = singlePDF.Import <SautinSoft.Document.Section>(sourceSection, true, session); if (dc.Sections.IndexOf(sourceSection) == 0) { importedSection.PageSetup.SectionStart = SectionStart.NewPage; } singlePDF.Sections.Add(importedSection); } } singlePDF.Save(singlePDFPath); ////try4 //string[] inputFilePaths = Directory.GetFiles(txtFolder.Text, "first.jpg"); //Console.WriteLine("Number of files: {0}.", inputFilePaths.Length); //using (var outputStream = File.Create(txtFolder.Text)) //{ // foreach (var inputFilePath in inputFilePaths) // { // using (var inputStream = File.OpenRead(inputFilePath)) // { // // Buffer size can be passed as the second argument. // inputStream.CopyTo(outputStream); // } // Console.WriteLine("The file {0} has been processed.", inputFilePath); // } //} }
/// <summary> /// How to convert all files to a single XLS file. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/from-customers-convert-pdf-docx-rtf-to-single-xls-workbook-net-csharp-vb.php /// </remarks> public static void ConvertToSingleXls() { // In this example we'll use not only Document .Net component, but also // another SautinSoft 'component - PDF Focus .Net (to perform conversion from PDF to single xls workbook). // First of all, please perform "Rebuild Solution" to restore PDF Focus .Net package from NuGet. // Our steps: // 1. Convert all RTF, DOCX, PDF files into a single PDF document. (by Document .Net). // 2. Convert the single PDF into a single XLS workbook. (by PDF Focus .Net). byte[] singlePdfBytes = null; // This file we need only to show intermediate result. string singlePdfFile = "Single.pdf"; string workingDir = @"..\..\"; string singleXlsFile = "Single.xls"; List <string> supportedFiles = new List <string>(); foreach (string file in Directory.GetFiles(workingDir, "*.*")) { string ext = Path.GetExtension(file).ToLower(); if (ext == ".pdf" || ext == ".docx" || ext == ".rtf") { supportedFiles.Add(file); } } // Create single pdf. DocumentCore singlePDF = new DocumentCore(); foreach (string file in supportedFiles) { DocumentCore dc = DocumentCore.Load(file); Console.WriteLine("Adding: {0}...", Path.GetFileName(file)); // Create import session. ImportSession session = new ImportSession(dc, singlePDF, StyleImportingMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section sourceSection in dc.Sections) { // Because we are copying a section from one document to another, // it is required to import the Section into the destination document. // This adjusts any document-specific references to styles, bookmarks, etc. // // Importing a element creates a copy of the original element, but the copy // is ready to be inserted into the destination document. Section importedSection = singlePDF.Import <Section>(sourceSection, true, session); // First section start from new page. if (dc.Sections.IndexOf(sourceSection) == 0) { importedSection.PageSetup.SectionStart = SectionStart.NewPage; } // Now the new section can be appended to the destination document. singlePDF.Sections.Add(importedSection); } } // Save our single document into PDF format in memory. // Let's save our document to a MemoryStream. using (MemoryStream Pdf = new MemoryStream()) { singlePDF.Save(Pdf, new PdfSaveOptions() { Compliance = PdfCompliance.PDF_A1a }); singlePdfBytes = Pdf.ToArray(); } // Open the result for demonstration purposes. File.WriteAllBytes(singlePdfFile, singlePdfBytes); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singlePdfFile) { UseShellExecute = true }); SautinSoft.PdfFocus f = new PdfFocus(); f.OpenPdf(singlePdfBytes); if (f.PageCount > 0) { f.ToExcel(singleXlsFile); } // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singleXlsFile) { UseShellExecute = true }); }
/// <summary> /// This sample shows how to merge multiple files DOCX, PDF into single document in memory. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/merge-multiple-documents-docx-pdf-in-memory-net-csharp-vb.php /// </remarks> public static void MergeDocumentsInMem() { // We'll use these files only to get data and show the result. // The whole merging process will be done in memory. string[] documents = new string[] { @"..\..\one.docx", @"..\..\two.pdf" }; string singleDocumentPath = "Result.pdf"; // Read the files and retrieve the file data into this Dictionary. // Thus we'll have input data completely in memory. Dictionary <string, byte[]> documentsData = new Dictionary <string, byte[]>(); foreach (string file in documents) { documentsData.Add(file, File.ReadAllBytes(file)); } // Merge documents in memory (using MemoryStream) // 1. Create a single document. DocumentCore dcSingle = new DocumentCore(); foreach (KeyValuePair <string, byte[]> document in documentsData) { // Create new MemoryStream based on document byte array. using (MemoryStream msDoc = new MemoryStream(document.Value)) { LoadOptions lo = null; if (Path.GetExtension(document.Key).ToLower() == ".docx") { lo = new DocxLoadOptions(); } else if (Path.GetExtension(document.Key).ToLower() == ".pdf") { lo = new PdfLoadOptions() { PreserveEmbeddedFonts = false } } ; DocumentCore dc = DocumentCore.Load(msDoc, lo); Console.WriteLine("Adding: {0}...", Path.GetFileName(document.Key)); // Create import session. ImportSession session = new ImportSession(dc, dcSingle, StyleImportingMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section sourceSection in dc.Sections) { // Because we are copying a section from one document to another, // it is required to import the Section into the destination document. // This adjusts any document-specific references to styles, bookmarks, etc. // // Importing a element creates a copy of the original element, but the copy // is ready to be inserted into the destination document. Section importedSection = dcSingle.Import <Section>(sourceSection, true, session); // First section start from new page. if (dc.Sections.IndexOf(sourceSection) == 0) { importedSection.PageSetup.SectionStart = SectionStart.NewPage; } // Now the new section can be appended to the destination document. dcSingle.Sections.Add(importedSection); } } } // Save the resulting document as PDF into MemoryStream. using (MemoryStream msPdf = new MemoryStream()) { dcSingle.Save(msPdf, new PdfSaveOptions()); // Let's also save our document to PDF file for showing the result. File.WriteAllBytes(singleDocumentPath, msPdf.ToArray()); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singleDocumentPath) { UseShellExecute = true }); } } }