2010-10-10 19 views
9

Muszę scalić wiele plików DOCX (utworzonych z DocX Package) w jeden duży plik. Udało mi się już scalić wiele plików, ale nie udało mi się scalić nagłówków i stopek (różnią się one między plikami, stronami). Próbowałem zarówno DocX package i MS Office COM Interop i po prostu nie chce scalić nagłówków/stopek. Są pomijane.Scalanie plików docx razem z nagłówkami, stopkami i obrazkami

Czy ktoś może zapewnić rozwiązanie, które zadziała? Może to być naprawdę wszystko (w tym konwersja do formatu PDF za pomocą COM PDFCreator lub Interop lub DocX).

Próbowałem również PDFCreator Sample w języku C# i konwertuje ładnie jeden dokument do formatu PDF, ale nie wiem, jak dostarczyć wiele dokumentów, aby utworzyć jeden duży plik PDF. Więc jeśli ktoś może zapewnić dobry (wolny) sposób, będzie to docenione.

To co mam użyć:

internal static class DocumentsMerging { 
    private static object missing = Type.Missing; 
    private static ApplicationClass wordApplication { get; set; } 
    private static void addDocument(object path, Document doc, bool firstDocument) { 
     object subDocPath = path; 
     var subDoc = wordApplication.Documents.Open(ref subDocPath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
     try { 
      if (!firstDocument) { 
       insertPageBreak(doc); 
      } 
      object docStart = doc.Content.End - 1; 
      object docEnd = doc.Content.End; 
      object start = subDoc.Content.Start; 
      object end = subDoc.Content.End; 
      Range rng = doc.Range(ref docStart, ref docEnd); 
      rng.FormattedText = subDoc.Range(ref start, ref end); 
      //if (!lastDocument) { 
      //} 
     } finally { 
      subDoc.Close(ref missing, ref missing, ref missing); 
     } 
    } 
    public static bool deleteFile(string fileName) { 
     if (File.Exists(fileName)) { 
      try { 
       File.Delete(fileName); 
       if (File.Exists(fileName)) { 
        return false; 
       } 
       return true; 
      } catch (IOException) { 
       DialogResult result = MessageBox.Show(new Form {TopMost = true}, "Plik " + fileName + " jest w użyciu lub nie masz uprawnień do zapisania raportu w tym miejscu. Czy chcesz spróbować ponownie?", "Błąd zapisu (000002)", MessageBoxButtons.YesNo, MessageBoxIcon.Stop); 
       if (result == DialogResult.No) { 
        return false; 
       } 
       if (deleteFile(fileName)) { 
        return true; 
       } 
      } catch (Exception e) { 
       MessageBox.Show(new Form {TopMost = true}, "Plik " + fileName + " nie może zostać skasowany. Błąd " + Environment.NewLine + Environment.NewLine + e, "Błąd zapisu (000003)", MessageBoxButtons.YesNo, MessageBoxIcon.Stop); 
       return false; 
      } 
     } else { 
      return true; 
     } 
     return false; 
    } 
    public static void documentsMerge(object fileName, List<string> arrayList) { 
     // object fileName = Path.Combine(Environment.CurrentDirectory, @"NewDocument.doc"); 
     bool varTest = deleteFile(fileName.ToString()); 
     if (varTest) { 
      try { 
       wordApplication = new ApplicationClass(); 
       var doc = wordApplication.Documents.Add(ref missing, ref missing, ref missing, ref missing); 
       try { 
        doc.Activate(); 
        int count = 0; 
        foreach (var alItem in arrayList) { 
         addDocument(alItem, doc, count == 0); 
         count++; 
        } 
        // addDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc1.doc", doc) ; //, false); 
        // addDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc2.doc", doc) ; //, true); 
        doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
       } finally { 
        doc.Close(ref missing, ref missing, ref missing); 
       } 
      } finally { 
       wordApplication.Quit(ref missing, ref missing, ref missing); 
      } 
     } else { 
      return; 
     } 
    } 
    private static void insertPageBreak(Document doc) { 
     object docStart = doc.Content.End - 1; 
     object docEnd = doc.Content.End; 
     Range rng = doc.Range(ref docStart, ref docEnd); 
     // object pageBreak = WdBreakType.wdPageBreak; 
     object pageBreak = WdBreakType.wdSectionBreakNextPage; 

     rng.InsertBreak(ref pageBreak); 
    } 
} 

Odpowiedz

6

to sprawdzić http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx

Zmień ten wiersz:

object pageBreak = Word.WdBreakType.wdPageBreak; 

do tego

object pageBreak = Word.WdBreakType.wdSectionBreakNextPage; 

Aby uzyskać HEA ding i stopka z drugiego dokumentu.

Dodanie całej metody i sposobu jej wywołania w przypadku zmiany źródła.

Klasa:

using System; 
using Word = Microsoft.Office.Interop.Word; 
using System.Configuration; 

namespace KeithRull.Utilities.OfficeInterop 
{ 
    public class MsWord 
    { 
    /// <summary> 
    /// This is the default Word Document Template file. I suggest that you point this to the location 
    /// of your Ms Office Normal.dot file which is usually located in your Ms Office Templates folder. 
    /// If it does not exist, what you could do is create an empty word document and save it as Normal.dot. 
    /// </summary> 
    private static string defaultWordDocumentTemplate = @"Normal.dot"; 

    /// <summary> 
    /// A function that merges Microsoft Word Documents that uses the default template 
    /// </summary> 
    /// <param name="filesToMerge">An array of files that we want to merge</param> 
    /// <param name="outputFilename">The filename of the merged document</param> 
    /// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param> 
    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks) 
    { 
     Merge(filesToMerge, outputFilename, insertPageBreaks, defaultWordDocumentTemplate); 
    } 

    /// <summary> 
    /// A function that merges Microsoft Word Documents that uses a template specified by the user 
    /// </summary> 
    /// <param name="filesToMerge">An array of files that we want to merge</param> 
    /// <param name="outputFilename">The filename of the merged document</param> 
    /// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param> 
    /// <param name="documentTemplate">The word document you want to use to serve as the template</param> 
    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate) 
    { 
     object defaultTemplate = documentTemplate; 
     object missing = System.Type.Missing; 
     object pageBreak = Word.WdBreakType.wdSectionBreakNextPage; 
     object outputFile = outputFilename; 

     // Create a new Word application 
     Word._Application wordApplication = new Word.Application(); 

     try 
     { 
      // Create a new file based on our template 
      Word.Document wordDocument = wordApplication.Documents.Add(
              ref missing 
             , ref missing 
             , ref missing 
             , ref missing); 

      // Make a Word selection object. 
      Word.Selection selection = wordApplication.Selection; 

      //Count the number of documents to insert; 
      int documentCount = filesToMerge.Length; 

      //A counter that signals that we shoudn't insert a page break at the end of document. 
      int breakStop = 0; 

      // Loop thru each of the Word documents 
      foreach (string file in filesToMerge) 
      { 
       breakStop++; 
       // Insert the files to our template 
       selection.InsertFile(
              file 
             , ref missing 
             , ref missing 
             , ref missing 
             , ref missing); 

       //Do we want page breaks added after each documents? 
       if (insertPageBreaks && breakStop != documentCount) 
       { 
        selection.InsertBreak(ref pageBreak); 
       } 
      } 

      // Save the document to it's output file. 
      wordDocument.SaveAs(
          ref outputFile 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing 
         , ref missing); 

      // Clean up! 
      wordDocument = null; 
     } 
     catch (Exception ex) 
     { 
      //I didn't include a default error handler so i'm just throwing the error 
      throw ex; 
     } 
     finally 
     { 
      // Finally, Close our Word application 
      wordApplication.Quit(ref missing, ref missing, ref missing); 
     } 
    } 
} 
} 

i wezwanie:

using System; 
using KeithRull.Utilities.OfficeInterop; 

namespace WordDocMerge2 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     try 
     { 
      string document1 = @"D:\Visual Studio Projects\31.docx"; 
      string document2 = @"D:\Visual Studio Projects\33.docx"; 
      string document3 = @"D:\Visual Studio Projects\32.docx"; 

      string[] documentsToMerge = { document1, document2, document3 }; 

      string outputFileName = String.Format("D:\\Visual Studio Projects\\{0}.docx", Guid.NewGuid()); 

      MsWord.Merge(documentsToMerge, outputFileName, true); 

     } 
     catch (Exception ex) 
     { 
      //messageLabel.Text = ex.Message; 
     } 
    } 
} 
} 
+0

Niestety nie łączy to stopek i nagłówków z tym, co pamiętam, ponieważ pamiętam, jak próbowałem go jakiś czas temu. – MadBoy

+0

Ok, będę majstrować przy tym rozwiązaniu, aby sprawdzić, czy można go zmodyfikować, aby to zrobić. – darkstar3d

+0

Przepraszamy za opóźnienie, praca nadrobiła we mnie. Jeśli zmienisz linię przerywania strony w klasie rozwiązania, które również podłączyłem, to naprawi to za Ciebie. Zmień go na: object pageBreak = Word.WdBreakType.wdSectionBreakNextPage; – darkstar3d

0

Czy próbowałeś się połączyć na poziomie xml? Docx to pliki zip z plikami Xml.

Być może będziesz w stanie scalić XML za pomocą XDocument.

+0

Nie mam. Naprawdę nie wiem, jak byłoby wspaniale, gdybyś mógł to zrobić;) – MadBoy

0

Zacznij od kodu C# http://powertools.codeplex.com/ i zmodyfikuj go nieznacznie, aby spełniał wszystkie oczekiwania, korzystając z nagłówków i stopek.

Eric White wyjaśnia bloga zawiera komentarz na temat działania kodu.

4

Od newest version of DocX scalanie działa poprawnie. Zamiast korzystać z Interop, możliwe jest teraz bezpośrednie korzystanie z DocX. Jedyne, co należy wiedzieć, to: headers/footers pochodzą z pierwszego dokumentu (szablonu), ponieważ nagłówki/stopki są ustawiane na dokument, a nie na stronę.

public static void documentsMerge(object fileName, List<string> arrayList) { 

     bool varTest = deleteFile(fileName.ToString()); 
     if (varTest) 
     { 
      using (DocX documentToCreate = DocX.Load(arrayList[0])) 
      { 


       foreach (var alItem in arrayList.Skip(1)) 
       { 
        documentToCreate.InsertParagraph().InsertPageBreakAfterSelf(); 
        DocX documentToMergeIn = DocX.Load(alItem); 
        documentToCreate.InsertDocument(documentToMergeIn); 
       } 
       documentToCreate.SaveAs(fileName.ToString()); 
      } 
     } 
    } 
+0

Ty, mój przyjacielu, jesteś ratownikiem! – Nathan