0

I am creating an NSMutableAttributedString, attaching an image, getting string value from html representation and saving it, found from SO answer. It can get the relative file path on string. But If I save it as doc file it does not show image. Probably doc software can not find that location and doc's should not dynamically load data from file location. Is there any way to embed image while saving as doc file?

Heres what I'm doing:

let fullString = NSMutableAttributedString()

    // create our NSTextAttachment
    let image1Attachment = NSTextAttachment()
    image1Attachment.image = UIImage(named: "red-roses.jpg") //NSIMage 

    // wrap the attachment in its own attributed string so we can append it
    let image1String = NSAttributedString(attachment: image1Attachment)

    // add the NSTextAttachment wrapper to our full string, then add some more text.
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: "End of text"))

    // draw the result in a label
    label.attributedText = fullString
    
    print(fullString.mutableString)
    //imageView.image = UIImage(named: "red-roses")
    
    do {
                let htmlData = try fullString.data(from: .init(location: 0, length: fullString.length),
                                                       documentAttributes: [.documentType: NSAttributedString.DocumentType.html])
                let htmlString = String(data: htmlData, encoding: .utf8) ?? ""
                print(htmlString)
    //try htmlString.writeTo(fileLocation, "filename.doc")
     } catch {
         print(error)
     }

It can show relative file path on htmlString. The saved doc will show only strings and no image. Is there any way to embed image on doc?

Rifat
  • 1,700
  • 3
  • 20
  • 51
  • You can not embed an image inside a html file. It is just a String. Your only option is to convert your image data to base64EncodedString – Leo Dabus Oct 22 '20 at 16:38
  • Thank you for the hint. – Rifat Oct 22 '20 at 16:55
  • @LeoDabus The saved docx doesn't show the image from embedded string as base64 encoded and opening the app does not show any image, only text. – Rifat Oct 22 '20 at 17:38
  • You won't be able to do that using the method above. You will need to manually replace the image inside your html – Leo Dabus Oct 22 '20 at 17:38
  • How can I show embedded image in html saved as doc/docx? – Rifat Oct 22 '20 at 17:39
  • you need to replace `Attachment.png` by `Image Alternative Text` in your html string. don't forget to use jpg instead of png – Leo Dabus Oct 22 '20 at 17:41
  • I have tested in similar way. I saved an html document directly with solution from [this answer]( https://stackoverflow.com/a/8499716/7291498). Saved as docx. opened in wpf app and no image appeared, but text appeared. Image appears on webpage on a browser but same code as a doc no image on wpf mac app. – Rifat Oct 22 '20 at 17:44
  • DocX is a xml not a html – Leo Dabus Oct 22 '20 at 17:57
  • Not related to your question but why did you tag this as macOS and are trying to embed an UIImage instead of NSImage? – Leo Dabus Oct 22 '20 at 18:36
  • @LeoDabus I'm testing that on iOS with `UIKit`, tested same for macOS with `AppKit`, so I hinted `NSImage`. I did not tag for iOS but I think similar operation applies for both. – Rifat Oct 23 '20 at 05:15
  • From [this](https://www.codeproject.com/Questions/1271014/How-to-convert-base64-imgae-as-HTML-text-to-word-d), I'm taking result that doc can not show embedded images data/Base64Image. – Rifat Oct 23 '20 at 06:07

0 Answers0