示例#1
0
        public static void Test1()
        {
            string templatePath = @"C:\Users\Alim\Desktop\Templates\test1\Irrigation-Template - Copy.docx";
            string contentPath  = @"C:\Users\Alim\Desktop\Templates\test1\Irrigation-Content.docx";
            //TestInterOp.TestInterOpWord(templatePath, contentPath);

            XWPFDocument srcDoc = new XWPFDocument(new FileStream(templatePath, FileMode.OpenOrCreate));

            XWPFDocument destDoc = new XWPFDocument();

            // Copy document layout.
            CopyLayout(srcDoc, destDoc);

            Stream outStream = new FileStream("Destination.docx", FileMode.Create);


            foreach (IBodyElement bodyElement in srcDoc.BodyElements)
            {
                BodyElementType elementType = bodyElement.ElementType;

                if (elementType == BodyElementType.PARAGRAPH)
                {
                    XWPFParagraph srcPr = (XWPFParagraph)bodyElement;

                    //CopyStyle(srcDoc, destDoc, srcDoc.GetStyles().GetStyle(srcPr.StyleID));

                    bool hasImage = false;

                    XWPFParagraph dstPr = destDoc.CreateParagraph();

                    // Extract image from source docx file and insert into destination docx file.

                    foreach (XWPFRun srcRun in srcPr.Runs)
                    {
                        dstPr.CreateRun();

                        if (srcRun.GetEmbeddedPictures().Count > 0)
                        {
                            hasImage = true;
                        }

                        foreach (XWPFPicture pic in srcRun.GetEmbeddedPictures())
                        {
                            byte[] img = pic.GetPictureData().Data;

                            long cx = pic.GetCTPicture().spPr.xfrm.ext.cx;
                            long cy = pic.GetCTPicture().spPr.xfrm.ext.cy;

                            try
                            {
                                // Working addPicture Code below...
                                string blipId = dstPr.Document.AddPictureData(img, pic.GetHashCode());
                                destDoc.CreatePictureCxCy(blipId, destDoc.GetNextPicNameNumber(pic.GetHashCode()), cx, cy);
                            }
                            catch (FormatException e1)
                            {
                                Console.WriteLine(e1.StackTrace);
                            }
                        }
                    }



                    if (hasImage == false)
                    {
                        int pos = destDoc.Paragraphs.Count - 1;
                        destDoc.SetParagraph(srcPr, pos);
                    }
                }
                else if (elementType == BodyElementType.TABLE)
                {
                    //XWPFTable table = (XWPFTable)bodyElement;

                    //copyStyle(srcDoc, destDoc, srcDoc.getStyles().getStyle(table.getStyleID()));

                    //destDoc.createTable();

                    //int pos = destDoc.getTables().size() - 1;

                    //destDoc.setTable(pos, table);
                }
            }


            destDoc.Write(outStream);
            outStream.Close();
        }