This project has moved and is read-only. For the latest updates, please go here.

Combining Documents? (problem w/InsertDocument?)

Oct 5, 2011 at 6:28 PM

I'm getting a "Specified part does not exist in the package" exception when I try to combine four different DocX documents (each of them with images).  As it turns out, all the images are the same (well, there are three distinct images, but each document has those same three images) - is there a way I can just put those three images into the combined document and then updated all the references in the child document to use the images in the parent document?  I'm creating a very large word document and to speed things up I thought I'd split the job into as many cores as I have, generate the documents and then just concatenate them at the end.

Is there a way to do such a thing?

Here's some example code:

        static string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        static void Main(string[] args)
        {            
            int procCount = Environment.ProcessorCount;
            int qty = 500;
            var files = new string[procCount];//have a place for each of the filenames

            //1) create n filenames synchronously so we don't have any collisions
            for (int i = 0; i < procCount; i++)
            {
                var tempFileName = System.IO.Path.GetTempFileName();
                File.Delete(tempFileName);
                files[i] = System.IO.Path.ChangeExtension(tempFileName, ".docx");
            }

            var img1Path = Path.Combine(Desktop, "img1.jpg");
            var img2Path = Path.Combine(Desktop, "img2.jpg");
            var img3Path = Path.Combine(Desktop, "img3.jpg");

            //2) parallel for loop for each core
            Parallel.For(0, procCount, i =>
                {
                    Console.WriteLine("Thread {0}", i);

                    //3) create a 500 page docx file
                    using (var doc = DocX.Create(files[i]))
                    {
                        //add our images to the document
                        var img1 = doc.AddImage(img1Path);
                        var img2 = doc.AddImage(img2Path);
                        var img3 = doc.AddImage(img3Path);
                        Debug.WriteLine("Part {0}, Img1={1}; img2={2}; img3={3}", i, img1.Id, img2.Id, img3.Id);

                        for (int page = 0; page < qty; page++)
                        {
                            var pageContent = String.Format("Part {0}, Page {1}", i, page);
                            Console.WriteLine(pageContent);

                            var p = doc.InsertParagraph(pageContent);
                            p.AppendLine();
                            p.InsertPicture(img1.Id);
                            p.AppendLine("this is text after the picture");
                            p.InsertPicture(img2.Id);
                            p.AppendLine();
                            p.AppendLine("this is more text");
                            p.InsertPicture(img3.Id);
                            p.AppendLine("this is more text...again");
                            p.AppendLine("and again.");

                            p = doc.InsertParagraph("this is a new paragraph");
                            p.InsertPageBreakAfterSelf();
                        }

                        doc.Save();                        
                    }
                });


            //4) when that's all done, combine the individual files into a singe file
            var combinedPath = Path.Combine(Desktop, "combined.docx");
            using (var combined = DocX.Create(combinedPath))
            {
                Console.WriteLine("Combining documents...");
                var img1 = combined.AddImage(img1Path);
                var pic1 = img1.CreatePicture();

                for(int i = 0;i<procCount;i++)
                {
                    using(var doc = DocX.Load(files[i]))
                    {
                        //"Specified part does not exist in the package" thrown here:
                        combined.InsertDocument(doc);
                    }
                }
                combined.Save();
            }

            //5) launch the file
            Process.Start(combinedPath);
        }
    }
Dec 8, 2011 at 8:46 AM

Did you already resolve this issue? I was having the same issue and I came by a fix.

DocX.cs (line: 893)

old code:
PackagePart external_image_part = rel.Package.GetPart(new Uri("/word"+ uri_string, UriKind.RelativeOrAbsolute));

new code:
PackagePart external_image_part = rel.Package.GetPart(new Uri((uri_string.StartsWith("/word") ? "" : "/word") + uri_string, UriKind.RelativeOrAbsolute));

and then rebuild the project.

Dec 8, 2011 at 5:43 PM

Hi, It's the really hidden bag, I have applied your solution to the main trunk...

Dec 12, 2011 at 12:53 AM

Glad to be of help :)

Dec 12, 2011 at 3:48 PM
Edited Dec 12, 2011 at 3:49 PM

This has been a problem since a looong time as I reported it over a year ago (http://docx.codeplex.com/discussions/230066). Glad it's fixed now. Thank you @savior14.

Ps. DragonFire do you plan updating DocX manual? The last working/updated manual is from 1.0.0.10 and it's been a long time since it's up to date, and some things have heavily changed.

Dec 15, 2011 at 8:30 AM

MadBoy, yes, we are going to make a new stable versiond and update the manual =)

Jun 9, 2012 at 9:13 PM

DragonFire can you please contact me by email. I've something for you that may be of use for you :-)

Nov 20, 2012 at 2:46 AM
Edited Nov 20, 2012 at 3:02 AM
O_O 5months... sorry for that. havent logged in hotmail since that time. please cc this e-add:
newtrive@yahoo.com
Thanks,
Alvin
Dec 12, 2012 at 12:18 PM

I was asking DragonFire for a contact :-) Not you ;-p Unless you are DragonFire ;p