2017-09-28 6 views
1

J'utilise Novacode Docx pour créer un document Word avec C#, mais j'ai un problème avec la fusion de deux documents en un seul. Selon mon exigence, je dois télécharger deux documents Word sans les compresser à la fois. Cependant, je n'ai pas trouvé une solution pour cela, j'ai prévu de fusionner les documents Word et de les télécharger en un seul fichier. Peut-on m'aider à fusionner des documents Word en utilisant Novacode Docx.Novacode Docx Fusionner plusieurs mots Documents

Merci d'avance.

Répondre

1

je le fais comme ceci:

private DocX fullReportDocument; 
private DocX titleDocument; 
private DocX firstDocument; 
private DocX secondDocument; 

public byte[] GetReport(bool WantFirstReport, bool WantSecondReport) 
{ 

    fullDocument = DocX.Create(new MemoryStream()); 
    // Create title for the report 
    this.GenerateTitleDocument(); 
    // Insert the old document into the new document. 
    fullDocument.InsertDocument(titleDocument); 

    // Save the new document. 
    fullDocument.Save(); 

    if (WantFirstReport) 
    { 
     // Create expertise report 
     this.GenrateFirstDocument(); 

     // Insert a page break at the beginning to separate title and expertise report properly 
     firstDocument.Paragraphs[0].InsertPageBreakBeforeSelf(); 
     firstDocument.Save(); 

     // Insert the old document into the new document. 
     fullDocument.InsertDocument(firstDocument); 

     // Save the new document. 
     fullDocument.Save(); 
    } 

    if (WantSecondReport) 
    { 
     // Create expertise report 
     this.GenrateSecondDocument(); 

     // Insert a page break at the beginning to separate title and expertise report properly 
     secondDocument.Paragraphs[0].InsertPageBreakBeforeSelf(); 
     secondDocument.Save(); 

     // Insert the old document into the new document. 
     fullDocument.InsertDocument(secondDocument); 

     // Save the new document. 
     fullDocument.Save(); 
    } 
    return fullReportDocument.DocumentMemoryStream.ToArray(); 
} 

Mais j'ai modifié la propriété librairie DocX ajouter DocumentMemoryStream qui expose le MemoryStream interne MemoryStream

Si vous ne voulez pas modifier la bibliothèque DocX vous pouvez également faire ceci:

fullDocument.SaveAs(GENERATED_DOCX_LOCATION); 
return System.IO.File.ReadAllBytes(GENERATED_DOCX_LOCATION); 

GENERATED_DOCX_LOCATION est évidemment une chaîne avec le chemin physcal du fichier que vous souhaitez enregistrer.