2016-10-14 1 views
3

를 사용하여 Word 문서에 이미지를 대체 할 수 있지만, 난 그냥이 코드가 삽입 알고어떻게 내가 Novacode DOCX를 사용하여 Word 문서의 이미지를 교체 할 Novacode DOCX

var gDoc = DocX.Load(@"MauPhieuNhapCBCC.docx"); 
using (MemoryStream ms = new MemoryStream()) 
     { 
      System.Drawing.Image myImg = System.Drawing.Image.FromFile(@"img.jpg"); 

      myImg.Save(ms, myImg.RawFormat); // Save your picture in a memory stream. 
      ms.Seek(0, SeekOrigin.Begin); 

      Image img = gDoc.AddImage(ms); // Create image. 
      Picture pic1 = img.CreatePicture(); 
      // Create picture. 
      var abc = gDoc.Paragraphs.FirstOrDefault(g => g.Pictures.Count > 0); 
      abc.Alignment = Alignment.right; 
      abc.InsertPicture(pic1,29).Position(0.3); // Insert picture into paragraph. 
     } 
     gDoc.SaveAs("Exported.docx"); 

내가 대체하지 못할 수있는 방법 또는 특별한 위치에 삽입 하시겠습니까?

는 이 같은 그것을 할 수

답변

1

, 그것은 이상적인 방법하지 않을 수 있지만 제대로 작동 : 여기

var gDoc = DocX.Load(@"MauPhieuNhapCBCC.docx"); 
for (int f = 0; f < gDoc.Images.Count; f++) //Loop through the images of the document 
      { 
       Novacode.Image imo = gDoc.Images[f]; //Get image of the docuemnt 
       if (imo.Id == "imageIdInDocument") //Check the id 
       { 
        using (SD.Image newImg = SD.Image.FromFile("newImagePath")) //Load the new image for replacement 
        { 
         SD.Bitmap bMa = new SD.Bitmap(imo.GetStream(FileMode.Open, FileAccess.ReadWrite)); 
         SD.Graphics graf = SD.Graphics.FromImage(bMa); 
         graf.Clear(SD.Color.White); //Set the image of the document all white 

         //Below we insert our new image over the one in the document 
         //We might need to change the image size to fit the area of our existing image 
         //If that were the case we give a function to resize images 
         SD.Image newResizeImg = meths.ResizeImage(newImg, newWidth, newHeight); 

         //Below we set the point(0, 0) to overlap the new image from the top left corner 
         graf.DrawImage(newResizeImg, new SD.Rectangle(new SD.Point(0, 0), newResizeImg.Size), 
         new SD.Rectangle(new SD.Point(), newResizeImg.Size), SD.GraphicsUnit.Pixel); 
         bMa.Save(imo.GetStream(FileMode.Create, FileAccess.Write), SD.Imaging.ImageFormat.Jpeg); 
        } 

       } 
      } 

이미지 크기를 조정하는 기능을 :에 있었다 우리가 기본적으로 한

public SD.Image ResizeImage(SD.Image img, int newWidth, int newHeight) 
    { 
     if (img.Width < newWidth && img.Height < newHeight) return img; 
     using (img) 
     { 
      SD.Bitmap cpy = new SD.Bitmap(newWidth, newHeight, SD.Imaging.PixelFormat.Format32bppArgb); 
      using (SD.Graphics gr = SD.Graphics.FromImage(cpy)) 
      { 
       gr.Clear(SD.Color.Transparent); 
       gr.InterpolationMode = SD.Drawing2D.InterpolationMode.HighQualityBicubic; 
       gr.DrawImage(img, new SD.Rectangle(0, 0, newWidth, newHeight), new SD.Rectangle(0, 0, img.Width, img.Height), SD.GraphicsUnit.Pixel); 
      } 
      return cpy; 
     } 
    } 

문서의 이미지를 가져 와서 흰색으로 칠하고 같은 크기 이하의 기존 이미지 안에 새 이미지를 설정합니다. 희망이 있으면 도움이됩니다.

관련 문제