/** * Adds a picture to the document. * * @param pictureData The picture data * @param format The format of the picture. * * @return the index to this picture (0 based), the Added picture can be obtained from {@link #getAllPictures()} . * @throws InvalidFormatException */ public String AddPictureData(byte[] pictureData, int format) { XWPFPictureData xwpfPicData = document.FindPackagePictureData(pictureData, format); POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format]; if (xwpfPicData == null) { /* Part doesn't exist, create a new one */ int idx = document.GetNextPicNameNumber(format); xwpfPicData = (XWPFPictureData)CreateRelationship(relDesc, XWPFFactory.GetInstance(), idx); /* write bytes to new part */ PackagePart picDataPart = xwpfPicData.GetPackagePart(); Stream out1 = null; try { out1 = picDataPart.GetOutputStream(); out1.Write(pictureData, 0, pictureData.Length); } catch (IOException e) { throw new POIXMLException(e); } finally { try { if (out1 != null) { out1.Dispose(); } } catch (IOException) { // ignore } } document.RegisterPackagePictureData(xwpfPicData); pictures.Add(xwpfPicData); return(GetRelationId(xwpfPicData)); } else if (!GetRelations().Contains(xwpfPicData)) { /* * Part already existed, but was not related so far. Create * relationship to the already existing part and update * POIXMLDocumentPart data. */ PackagePart picDataPart = xwpfPicData.GetPackagePart(); // TODO add support for TargetMode.EXTERNAL relations. TargetMode targetMode = TargetMode.Internal; PackagePartName partName = picDataPart.PartName; String relation = relDesc.Relation; PackageRelationship relShip = GetPackagePart().AddRelationship(partName, targetMode, relation); String id = relShip.Id; AddRelation(id, xwpfPicData); pictures.Add(xwpfPicData); return(id); } else { /* Part already existed, Get relation id and return it */ return(GetRelationId(xwpfPicData)); } }
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(); }