none
Images uploading into the wrong folder? RRS feed

  • Question

  • Hi all,

    I'm trying to get a project that uses the Custom XML and fields to produce a report. I had 80% of this working fine with the old IO.Packaging but ran into issues trying to add files as embedded objects.

    DocumentFormat.OpenXML seems the nice way to go by far so I'm attempting to convert mode code to use it instead.

    I'd really appricate if someone could point me in the direction of a complete example of how to get into an existing word doc and edit the xml and then save all the changes. I've been looking and there are heaps of "how to's" but they're very limited in what they show.

    Also I'm adding a new image part but it seems to be creating a new media folder in the root of the word doc rather than in the word folder (ie. /media rather than /word/media). Am I missing something?

    Dim imagePart As ImagePart = p_mainDocPart.AddImagePart(ImagePartType.Jpeg)  
     
    'image comes from the database  
    Dim imageStream As Stream = imagePart.GetStream  
    Dim len As Integer = Convert.ToInt32(l_fileData.Length)  
    Dim bytesRead As Integer = l_fileData.Length  
    If bytesRead = len Then 
        imageStream.Write(l_fileData, 0, len)  
    End If 
     
    imagePart.FeedData(imageStream)  
     
    'Grab new Id for linking in the Document.xml  
    Dim l_newRelId As String = p_mainDocPart.GetIdOfPart(imagePart) 

    Cheers
    Monday, December 1, 2008 5:55 AM

Answers

  • I ended up using;

    Dim l_imagePart As ImagePart = m_wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg)  
     
    Using l_memStrm As New MemoryStream(l_fileData)  
       l_imagePart.FeedData(l_memStrm)  
    End Using 

    The images still go into /media rather than /word/media however the references are ok.
    • Marked as answer by Jargo Tuesday, December 9, 2008 12:59 AM
    Tuesday, December 9, 2008 12:59 AM

All replies

  • Hi Jargo,

    About inserting a new image, there is a HOWTO article show you how to do that: http://msdn.microsoft.com/en-us/library/bb497430(office.14).aspx. You only need to write the following few lines of code:

    1 ' To add a new image part to a package.  
    2 Public Sub AddImagePart(ByVal document As StringByVal fileName As String)  
    3    Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, True)  
    4    Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart  
    5    Dim imagePart As ImagePart = mainPart.AddImagePart(ImagePartType.Jpeg)  
    6    Dim stream As FileStream = New FileStream(fileName, FileMode.Open)  
    7    imagePart.FeedData(stream)  
    8 End Sub 

    Notice, this article only works with V2 of the Open XML SDK.

    About the custom XML modification, there is a HOWTO article on how to read it: Article: Reading Custom XML Data in WordprocessingML, which is definitely part of your work.

    Could you tell me more about your project's workflow, say, where is the input from, what processing staff you need to take, and the requirement to the final results?


    xinxing
    Monday, December 1, 2008 12:38 PM
  • Thanks Xinxing,

    The how to on adding images is the one I used to base my code on. The differences being that I have the image from the database rather than on the file system. Yet it still doesn't put the media folder in the right place. Is there any way to set a target or something?

    Basically I'm creating a word document as a report collated from data entered into a web app. I have a series of tables that get repeated for each aditional bit of data. For example one of these tables lists all the items and data from a checklist of variable length. My code grabs the first row of the table as a template and then re-inserts it for each new checklist item. Within in of this there are data fields mapped to a custom xml file to bring though the data.

    So I need to open the file in memory, add images and embedded files, insert the new custom xml file, update references and insert new xml to the Document.xml, and then return the whole lot to the user on the response for them to open or save.

    Does that give you a better idea of what I'm trying to achieve?

    Cheers
    Monday, December 1, 2008 10:50 PM
  • Ok, I've sorted out the flow of the updates and edits now. I get an copy of the MainDocumentPart's xml and edit that then save that back to the part just once. It all seems to be working like it did using the IO.Packaging, finally.

    Still having issues with the images not being saved to the right folder. The image also seems to be missing a reference in either the .rels or the document.xml.rels, and if you get the properties of the image it lists the compression as 'deflated' rather than 'stored'.

    All of this leads to one corrupted Word Doc.

    Dim l_fileData() As Byte = l_row.Item("Attachment")  
     
    Dim imagePart As ImagePart = m_wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg)  
     
    Using outputStream As Stream = imagePart.GetStream  
        Dim len As Integer = Convert.ToInt32(l_fileData.Length)  
        Dim bytesRead As Integer = l_fileData.Length  
        If bytesRead = len Then 
             outputStream.Write(l_fileData, 0, len)  
        End If 
    End Using 

    Any ideas? Should I be using imagePart.FeedBytes() somehow?
    Tuesday, December 2, 2008 5:26 AM
  • I ended up using;

    Dim l_imagePart As ImagePart = m_wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg)  
     
    Using l_memStrm As New MemoryStream(l_fileData)  
       l_imagePart.FeedData(l_memStrm)  
    End Using 

    The images still go into /media rather than /word/media however the references are ok.
    • Marked as answer by Jargo Tuesday, December 9, 2008 12:59 AM
    Tuesday, December 9, 2008 12:59 AM
  • I am having the same issue...Can you help?

     

    Image going to wrong folder...and also need help with replacing image....

    Thursday, December 15, 2011 7:28 PM