Images inside documents are not correctly Inserted while using "InsertDocument"

Apr 24, 2012 at 9:18 AM
Edited Apr 24, 2012 at 9:21 AM

Hi, 

I am trying to merge documents using InsertDocument method. Please find the code below, 

 

        public DocX Merge(List<DocX> documents)
        {
            var baseDocument = documents.First();

            documents.SkipWhile(x => x == baseDocument).ToList().ForEach(x => baseDocument.InsertDocument(x));

            return baseDocument;
        }

 

It is working perfectly, but when there are any images in any of the documents, then the image is not correctly inserted, and is showing there an X mark with message "The image cannot currently be displayed". This is happening only when the source document has image without any text or other elements in it. Otherwise the images are getting copied in the merged document perfectly. Any workaround for this. Thanks!!! I am using the latest version of the DocX Built from the sourcecode. 

Developer
Apr 24, 2012 at 9:23 AM

Have you used newest source code that you compiled yourself or older version? If not try to compile newest source code and report results.

Apr 24, 2012 at 9:24 AM

Hi MadBoy,

Yes I downloaded the sourcecode today and using it compiled on my machine. 

Developer
Apr 24, 2012 at 9:26 AM

I'm using this code on preety complicated documents. Can you check if it works with this one?

 

public static void documentsMerge(object fileName, List<string> arrayList) {
           // MsWord.Merge(arrayList, (string) fileName, true);
            bool varTest = deleteFile(fileName.ToString());
            if (varTest)
            {
                using (DocX documentToCreate = DocX.Load(arrayList[0]))
                {


                    foreach (var alItem in arrayList.Skip(1))
                    {
                        documentToCreate.InsertParagraph().InsertPageBreakAfterSelf();
                        DocX documentToMergeIn = DocX.Load(alItem);
                        documentToCreate.InsertDocument(documentToMergeIn);
                    }
                    documentToCreate.SaveAs(fileName.ToString());
                }
            }
        }

 

Apr 24, 2012 at 9:35 AM

Thanks, its working nice. I like the idea of page break before each insertion. Thanks for the help. Cheers :)

Raj.

Feb 6, 2014 at 1:37 PM
Edited Feb 6, 2014 at 1:46 PM
I have the same error. Using your example collided with another mistake :( If someone knows how to fix this I would be very grateful.
    Public Sub Merge(ByVal arrayList As List(Of String))

        Using documentToCreate As DocX = DocX.Load(arrayList(0))
            For Each allItem In arrayList.Skip(1)
                documentToCreate.InsertParagraph().InsertPageBreakAfterSelf()
                Dim documentToMergeIn = DocX.Load(allItem)
                documentToCreate.InsertDocument(documentToMergeIn) /// Exception "An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll"
            Next
            documentToCreate.SaveAs("alldoc.docx")
        End Using

    End Sub
Version DocX.dll : 1.0.1.13
Exception snapshot: StackTrace
   в System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
   в Novacode.DocX.merge_numbering(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote)
   в Novacode.DocX.InsertDocument(DocX remote_document)
   в BarcodeVB.Module1.Merge(List`1 arrayList) в C:\Users\a.verboloz\Documents\WorkSpace\Barcode\BarcodeVB\BarcodeVB\Module1.vb:строка 15
   в BarcodeVB.Module1.Main() в C:\Users\a.verboloz\Documents\WorkSpace\Barcode\BarcodeVB\BarcodeVB\Module1.vb:строка 72
   в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   в System.Threading.ThreadHelper.ThreadStart()