- Code: Select all
doc.SaveToFile(sRes, FileFormat.Doc);
錯訊是這個
A value with the specified key has already been added.
可以跟我說有可能是甚麼原因會導致於錯訊嗎?
此版本為
version 13.5.11
但以下版本卻沒有問題
version 11.12.2
框架版本為 Console App .NET6
doc.SaveToFile(sRes, FileFormat.Doc);
protected TextBodyPart GetBookmarkContent(Document doc, string bookmarkName)
{
TextBodyPart tbpRes = null;
BookmarksNavigator bkNav = new BookmarksNavigator(doc);
try
{
bkNav.MoveToBookmark(bookmarkName);
tbpRes = bkNav.GetBookmarkContent();
}
catch { tbpRes = null; }
return tbpRes;
}
public Paragraph CopyContent(Document srcDoc,
ref Document trgDoc,
Color fontColor,
string bookmarkName,
Dictionary<string, string> dicProps = null)
{
Paragraph firstPara = null;
if (srcDoc == null || trgDoc == null) return firstPara;
if (string.IsNullOrEmpty(bookmarkName)) return firstPara;
TextBodyPart srcTbp = GetBookmarkContent(srcDoc, bookmarkName);
if (srcTbp == null) return firstPara;
Section trgSec = trgDoc.LastSection;
if (trgSec == null) trgSec = trgDoc.AddSection();
Paragraph curPara = null;
bool ignorePicture = false;
foreach (DocumentObject docObj in srcTbp.BodyItems)
{
if (docObj.DocumentObjectType == DocumentObjectType.Paragraph)
{
var para = docObj as Paragraph;
var textAlignment = para.Format.TextAlignment;
curPara = trgSec.AddParagraph();
if (firstPara == null) firstPara = curPara;
foreach (DocumentObject childObj in docObj.ChildObjects)
{
try
{
if (childObj.DocumentObjectType == DocumentObjectType.OleObject)
{
if (((DocOleObject)childObj).ProgId == "Word.Picture.8")
{
DocOleObject Ole = childObj as DocOleObject;
if (Ole.NativeData != null)
{
//或者這段進行註解
DocOleObject oleObject = curPara.AppendOleObject(Ole.NativeData, Ole.OlePicture, Ole.LinkType);
oleObject.SetNativeData(Ole.NativeData);
oleObject.ObjectType = "Word.Document.8"; // 強制使用 Document
ignorePicture = true;
}
else
{
curPara.ChildObjects.Add(childObj.Clone());
}
}
else
{
curPara.ChildObjects.Add(childObj.Clone());
}
}
else if (childObj.DocumentObjectType == DocumentObjectType.Picture)
{
if (!ignorePicture)
{
curPara.ChildObjects.Add(childObj.Clone());
}
ignorePicture = false;
}
else if (childObj is Table)
{
Table table = childObj as Table;
Table clonedTable = table.Clone() as Table;
curPara.ChildObjects.Add(clonedTable);
}
else
{
DocumentObject bkObj = childObj.Clone(); //如果我把這段註解就正常了,但同樣的內文就會沒有貼上
curPara.ChildObjects.Add(bkObj); //如果我把這段註解就正常了,但同樣的內文就會沒有貼上
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
firstPara = null;
break;
}
}
}
else if (docObj.DocumentObjectType == DocumentObjectType.Table)
{
Table table = docObj as Table;
Table clonedTable = table.Clone() as Table;
if (clonedTable != null)
{
if (curPara == null)
{
trgSec.Body.ChildObjects.Add(clonedTable);
}
else
{
trgSec.Body.ChildObjects.Add(clonedTable);
}
}
}
}
return firstPara;
}
//聲明ignoreOleFieldMark
bool ignoreOleFieldMark=false;
......
if (Ole.NativeData != null)
{
DocOleObject oleObject = curPara.AppendOleObject(Ole.NativeData, Ole.OlePicture, Ole.LinkType);
oleObject.SetNativeData(Ole.NativeData);
oleObject.ObjectType = "Word.Document.8"; // 強制使用 Document
ignorePicture = true;
[color=#800000] ignoreOleFieldMark = true;[/color]
}
else
{
curPara.ChildObjects.Add(childObj.Clone());
}
}
else
{
curPara.ChildObjects.Add(childObj.Clone());
}
}
else if (childObj.DocumentObjectType == DocumentObjectType.FieldMark)
{
if (!ignoreOleFieldMark)
{
curPara.ChildObjects.Add(childObj.Clone());
}
if((childObj as FieldMark).Type==FieldMarkType.FieldEnd)
ignoreOleFieldMark = false;
}