예제 #1
0
        public void Bug57312_NullPointException()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("57312.docx");

            Assert.IsNotNull(doc);

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

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

                    foreach (IRunElement iRunElem in paragraph.IRuns)
                    {
                        if (iRunElem is XWPFRun)
                        {
                            XWPFRun RunElement = (XWPFRun)iRunElem;

                            UnderlinePatterns underline = RunElement.Underline;
                            Assert.IsNotNull(underline);

                            //System.out.Println("Found: " + underline + ": " + RunElement.GetText(0));
                        }
                    }
                }
            }
        }
예제 #2
0
        private int GetPosOfBodyElement(IBodyElement needle)
        {
            BodyElementType elementType = needle.ElementType;

            for (int index = 0; index < this.bodyElements.Count; ++index)
            {
                IBodyElement bodyElement = this.bodyElements[index];
                if (bodyElement.ElementType == elementType && bodyElement.Equals((object)needle))
                {
                    return(index);
                }
            }
            return(-1);
        }
예제 #3
0
        public bool RemoveBodyElement(int pos)
        {
            if (pos < 0 || pos >= this.bodyElements.Count)
            {
                return(false);
            }
            BodyElementType elementType = this.bodyElements[pos].ElementType;

            if (elementType == BodyElementType.TABLE)
            {
                int tablePos = this.GetTablePos(pos);
                this.tables.RemoveAt(tablePos);
                this.ctDocument.body.RemoveTbl(tablePos);
            }
            if (elementType == BodyElementType.PARAGRAPH)
            {
                int paragraphPos = this.GetParagraphPos(pos);
                this.paragraphs.RemoveAt(paragraphPos);
                this.ctDocument.body.RemoveP(paragraphPos);
            }
            this.bodyElements.RemoveAt(pos);
            return(true);
        }
예제 #4
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();
        }