我想將多個文檔合併成一個文檔,然後打開結果文檔並進一步處理它。使用AltChunks的合併文檔的空格爲空
「ChunkId」是每次調用此方法以增加唯一標識時增加的屬性。我遵循this site的例子。 這是用於合併多個文件(使用altchunks)代碼: `
private void MergeDocument(string mergePath, bool appendPageBreak)
{
if (!File.Exists(mergePath))
{
Log.Warn(string.Format("Document: \"{0}\" was not found.", mergePath));
return;
}
ChunkId++;
var altChunkId = "AltChunkId" + ChunkId;
var mainDocPart = DestinationDocument.MainDocumentPart;
if (mainDocPart == null)
{
DestinationDocument.AddMainDocumentPart();
mainDocPart = DestinationDocument.MainDocumentPart;
if (mainDocPart.Document == null)
mainDocPart.Document = new Document { Body = new Body() };
}
try
{
var chunk = mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
if (chunk != null)
using (var ms = new FileStream(mergePath, FileMode.Open))
{
chunk.FeedData(ms);
}
else
{
Log.Error(string.Format("Merge - Failed to create chunk document based on \"{0}\".", mergePath));
return; // failed to create chunk document, return from merge method
}
}
catch (Exception e)
{
Log.Error(string.Format("Merge - Failed to insert chunk document based on \"{0}\".", mergePath));
return; // failed to create chunk document, return from merge method
}
var altChunk = new AltChunk { Id = altChunkId };
//append the page break
if (appendPageBreak)
try
{
AppendPageBreak(mainDocPart);
Log.Info(string.Format("Successfully appended page break."));
}
catch (Exception ex)
{
Log.Error(string.Format("Eror appending page break. Message: \"{0}\".", ex.Message));
return; // return if page break insertion failed
}
// insert the document
var last = mainDocPart.Document
.Body
.Elements()
.LastOrDefault(e => e is Paragraph || e is AltChunk);
try
{
if (last == null)
mainDocPart.Document.Body.InsertAt(altChunk, 0);
else
last.InsertAfterSelf(altChunk);
Log.Info(string.Format("Successfully inserted new doc \"{0}\" into destination.", mergePath));
}
catch (Exception ex)
{
Log.Error(string.Format("Error merging document \"{0}\". Message: \"{1}\".", mergePath, ex.Message));
return; // return if the merge was not successfull
}
try
{
mainDocPart.Document.Save();
}
catch (Exception ex)
{
Log.Error(string.Format("Error saving document \"{0}\". Message: \"{1}\".", mergePath, ex.Message));
}
}`
如果我打開合併文檔與Word我可以看到它的內容(表格,文字,段落..),但如果我如果從代碼再次打開它說內部文本是「」(空字符串)。我需要內部文本來反映文檔包含的內容,因爲我必須用另一個文本替換一些佔位符,如「@@ name @@」,如果內部文本爲空,我不能。
這是合併文檔的innerxml,
我這是怎麼打開的合併文檔:
DestinationDocument = WordprocessingDocument.Open(Path.GetFullPath(destinationPath), true);
我如何可以讀取文檔的內部文本?或者我怎樣才能將這些文件合併成一個文件,以避免這個問題再發生?
謝謝你,工作就像一個魅力。要解決的唯一問題是如果可以在使用DocumentBuilder的文檔之後插入分頁符。好先生@Flowerking,再次感謝您:D – Simonca
您知道一種將.rtf文件轉換爲.docx文件的方式嗎?我需要將.rtf文檔與.docx合併,並且DocumentBuilder需要.docx文件。 – Simonca