現在、ユーザーが複数のWord文書を1つにマージして、書式やヘッダーなどを失うことのないプログラムで作業しています。ドキュメントは、何も変更せずに、次々にスタックしていくだけです。ここで WordProcessingDocumentを使用してWord文書をマージする
は私の現在のコードです:public virtual Byte[] MergeWordFiles(IEnumerable<SendData> sourceFiles)
{
int f = 0;
// If only one Word document then skip merge.
if (sourceFiles.Count() == 1)
{
return sourceFiles.First().File;
}
else
{
MemoryStream destinationFile = new MemoryStream();
// Add first file
var firstFile = sourceFiles.First().File;
destinationFile.Write(firstFile, 0, firstFile.Length);
destinationFile.Position = 0;
int pointer = 1;
byte[] ret;
// Add the rest of the files
try
{
using (WordprocessingDocument mainDocument = WordprocessingDocument.Open(destinationFile, true))
{
XElement newBody = XElement.Parse(mainDocument.MainDocumentPart.Document.Body.OuterXml);
for (pointer = 1; pointer < sourceFiles.Count(); pointer++)
{
WordprocessingDocument tempDocument = WordprocessingDocument.Open(new MemoryStream(sourceFiles.ElementAt(pointer).File), true);
XElement tempBody = XElement.Parse(tempDocument.MainDocumentPart.Document.Body.OuterXml);
newBody.Add(XElement.Parse(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(new Break { Type = BreakValues.Page })).OuterXml));
newBody.Add(tempBody);
mainDocument.MainDocumentPart.Document.Body = new Body(newBody.ToString());
mainDocument.MainDocumentPart.Document.Save();
mainDocument.Package.Flush();
}
}
}
catch (OpenXmlPackageException oxmle)
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, "Error while merging files. Document index {0}", pointer), oxmle);
}
catch (Exception e)
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, "Error while merging files. Document index {0}", pointer), e);
}
finally
{
ret = destinationFile.ToArray();
destinationFile.Close();
destinationFile.Dispose();
}
return ret;
}
}
ここでの問題は、フォーマットは、例えば第2の文書内の別のヘッダーがなることを意味し、最初の文書からコピーされ、すべての残りの部分に適用されていることです無視される。これをどうやって防ぐのですか?
私は、sectionMarkValues.NextPageを使用してセクションにドキュメントを分割し、altChunkを使用することを検討しています。
後者の問題は、altChunkはMemoryStreamを "FeedData"メソッドに処理できないようです。
あなたのSendDataオブジェクトが何のために働きますか? –
SendDataタイプには、プログラムで後で使用される情報(ファイルをディスクに保存するか電子メールで送信するかなど)が含まれます。マージには、バイト配列形式のドキュメントを含むFile属性のみが必要です。 –