UPDATE : Crazy, Clone() is not the root problem
1) docx with a shapegroup with two simple shapes
2) new document(this one)
3) savetofile docx with a new name --> exception
RQ : saving to PDF format does'nt throw the exception
- Code: Select all
public void DoSimpleTest()
{
string inputDocxName = Path.Combine(Application.StartupPath, "ShapeGroupTemplate.docx");
string outputDocxName = Path.Combine(Application.StartupPath, $"ShapeGroupTest {DateTime.Now:yyyy-MM-dd HH-mm-ss}.docx");
string outputPdfName = Path.Combine(Application.StartupPath, $"ShapeGroupTest {DateTime.Now:yyyy-MM-dd HH-mm-ss}.pdf");
using (Document inputDoc = new Document(inputDocxName))
{
inputDoc.SaveToFile(outputDocxName, FileFormat.Docx);
Process.Start(outputDocxName);
inputDoc.SaveToFile(outputPdfName, FileFormat.PDF);
Process.Start(outputPdfName);
inputDoc.Close();
}
}
I'm testing v8.1.10
My template docx contains a shapegroup with two simple shapes.
I need to copy (clone) this one in a new document docx.
When saving this new docx, I have an exception
"Impossible d'effectuer un cast d'un objet de type 'System.Drawing.Color' en type 'sprᭉ'."
- Code: Select all
public void DoTest()
{
string bookmarkName = "ShapeGroup";
string inputDocxName = Path.Combine(Application.StartupPath, "ShapeGroupTemplate.docx" );
string outputDocxName = Path.Combine(Application.StartupPath, $"ShapeGroupTest {DateTime.Now:yyyy-MM-dd HH-mm-ss}.docx");
string outputPdfName = Path.Combine(Application.StartupPath, $"ShapeGroupTest {DateTime.Now:yyyy-MM-dd HH-mm-ss}.pdf");
using (Document inputDocx = new Document(inputDocxName))
{
BookmarksNavigator nav = new BookmarksNavigator(inputDocx);
nav.MoveToBookmark(bookmarkName);
ShapeGroup shapeGroup = GetShapeGroup(nav);
using (Document outputDoc = new Document())
{
Section section = outputDoc.AddSection();
Paragraph paragraph = section.AddParagraph();
ShapeGroup clone = shapeGroup.Clone() as ShapeGroup;
paragraph.ChildObjects.Add(clone);
outputDoc.SaveToFile(outputDocxName, FileFormat.Docx);
Process.Start(outputDocxName);
outputDoc.SaveToFile(outputPdfName, FileFormat.PDF);
Process.Start(outputPdfName);
outputDoc.Close();
}
inputDocx.Close();
}
}
private ShapeGroup GetShapeGroup(BookmarksNavigator nav)
{
foreach (var item in nav.GetBookmarkContent().BodyItems)
{
if (item is Paragraph)
{
foreach (var item1 in (item as Paragraph).ChildObjects)
{
if (item1 is ShapeGroup)
{
return item1 as ShapeGroup;
}
}
}
}
return null;
}