2011-10-14 5 views
0

저는 OpenXMl sdk를 사용하여 C#을 통해 단어 문서를 만듭니다. 나는 모든 HTML 페이지를 워드 문서로 변환하고 있지만 변환 중에는 이미지에 대한 절대 주소를 부여하고 변환 한 후에는 내 시스템에 완벽하게 들어 있지만이 문서를 다른 시스템으로 가져 오려고 할 때 이미지가 나타나지 않습니다. 그곳에.Open XML을 통해 Word 문서를 만든 후 이미지가 나타나지 않습니다.

모든 이미지가 있지만 이름이 다른 미디어 디렉토리를 확인했습니다.

내 문서가 변환되었지만이 mathod를 사용 중입니다.

  using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentPath, true)) 
     { 
      XNamespace w = 
        "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; 
      XNamespace r = 
       "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; 
      string altChunkId = "AltChunkId1"; 
      MainDocumentPart mainPart = myDoc.MainDocumentPart; 
      AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId); 
      using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write)) 
      using (StreamWriter stringStream = new StreamWriter(chunkStream)) 
       stringStream.Write(html); 
      XElement altChunk = new XElement(w + "altChunk", 
       new XAttribute(r + "id", altChunkId) 
      ); 
      XDocument mainDocumentXDoc = GetXDocument(myDoc); 
      mainDocumentXDoc.Root 
       .Element(w + "body") 
       .Elements(w + "p") 
       .Last() 
       .AddAfterSelf(altChunk); 
      SaveXDocument(myDoc, mainDocumentXDoc); 
     } 





    private static XDocument GetXDocument(WordprocessingDocument myDoc) 
    { 
     // Load the main document part into an XDocument 
     XDocument mainDocumentXDoc; 
     using (Stream str = myDoc.MainDocumentPart.GetStream()) 
     using (XmlReader xr = XmlReader.Create(str)) 
      mainDocumentXDoc = XDocument.Load(xr); 
     return mainDocumentXDoc; 
    } 


    private static void SaveXDocument(WordprocessingDocument myDoc, 
XDocument mainDocumentXDoc) 
    { 
     // Serialize the XDocument back into the part 
     using (Stream str = myDoc.MainDocumentPart.GetStream(
      FileMode.Create, FileAccess.Write)) 
     using (XmlWriter xw = XmlWriter.Create(str)) 
      mainDocumentXDoc.Save(xw); 
    } 

이는 콘텐츠 및 절대 경로에 도시 된 afchunk.dat 파일을 생성한다.

기본적으로 나는 .html을 .docx 파일로 변환하려는 모든 코딩을 통해 파일을 만들고 싶지 않습니다.

그래서 아무도 HTML에서 오류가 발생하지 않고 어떻게 변환 할 수 있는지 말해 줄 수 있습니다.

답변

0

시도는 DocumentResource (항목 -> 새로운 추가)를 만들고이 이미지를 연결합니다.

전화 문서

using (Stream imgStream = ip.GetStream()) 
    { 
     System.Drawing.Bitmap logo = DocumentResources._default; 
     logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
    } 

Drawing drawing = BuildImage(imageRelationshipID, "_default.jpg", 200, 30); 

그리고 머리글 또는 바닥 글에 이미지를 구축 할 수있는 방법을 만들;

private static Drawing BuildImage(string imageRelationshipID, string imageName, int pixelWidth, int pixelHeight) 
    { 
     int emuWidth = (int)(pixelWidth * EMU_PER_PIXEL); 
     int emuHeight = (int)(pixelHeight * EMU_PER_PIXEL); 
     Drawing drawing = new Drawing(); 
     d.Wordprocessing.Inline inline = new d.Wordprocessing.Inline { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 }; 
     d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor(); 
     d.Wordprocessing.SimplePosition simplePos = new d.Wordprocessing.SimplePosition { X = 0, Y = 0 }; 
     d.Wordprocessing.Extent extent = new d.Wordprocessing.Extent { Cx = emuWidth, Cy = emuHeight }; 
     d.Wordprocessing.DocProperties docPr = new d.Wordprocessing.DocProperties { Id = 1, Name = imageName }; 
     d.Graphic graphic = new d.Graphic();    
     d.GraphicData graphicData = new d.GraphicData { Uri = GRAPHIC_DATA_URI }; 
     d.Pictures.Picture pic = new d.Pictures.Picture(); 
     d.Pictures.NonVisualPictureProperties nvPicPr = new d.Pictures.NonVisualPictureProperties(); 
     d.Pictures.NonVisualDrawingProperties cNvPr = new d.Pictures.NonVisualDrawingProperties { Id = 2, Name = imageName }; 
     d.Pictures.NonVisualPictureDrawingProperties cNvPicPr = new d.Pictures.NonVisualPictureDrawingProperties(); 
     d.Pictures.BlipFill blipFill = new d.Pictures.BlipFill(); 
     d.Blip blip = new d.Blip { Embed = imageRelationshipID }; 
     d.Stretch stretch = new d.Stretch(); 
     d.FillRectangle fillRect = new d.FillRectangle(); 
     d.Pictures.ShapeProperties spPr = new d.Pictures.ShapeProperties(); 
     d.Transform2D xfrm = new d.Transform2D(); 
     d.Offset off = new d.Offset { X = 0, Y = 0 }; 
     d.Extents ext = new d.Extents { Cx = emuWidth, Cy = emuHeight }; 
     d.PresetGeometry prstGeom = new d.PresetGeometry { Preset = d.ShapeTypeValues.Rectangle }; 
     d.AdjustValueList avLst = new d.AdjustValueList(); 
     xfrm.Append(off); 
     xfrm.Append(ext); 
     prstGeom.Append(avLst); 
     stretch.Append(fillRect); 
     spPr.Append(xfrm); 
     spPr.Append(prstGeom); 
     blipFill.Append(blip); 
     blipFill.Append(stretch); 
     nvPicPr.Append(cNvPr); 
     nvPicPr.Append(cNvPicPr); 
     pic.Append(nvPicPr); 
     pic.Append(blipFill); 
     pic.Append(spPr); 
     graphicData.Append(pic); 
     graphic.Append(graphicData); 
     inline.Append(extent); 
     inline.Append(docPr); 
     inline.Append(graphic); 
     drawing.Append(inline); 
     return drawing; 
    } 
관련 문제