Code Monkey home page Code Monkey logo

Comments (5)

tbaliance avatar tbaliance commented on August 29, 2024

There's a new AddDrawingInline method on Run that worked in my testing. I'll look at fleshing out the InlineDrawing later, but for now it's good enough to insert and allow you to control the image size.

from unioffice.

freb avatar freb commented on August 29, 2024

This definitely works. But I noticed I get an error when I try to open it in Word: We're sorry. We can't open out.docx because we found a problem with its contents. Once I click the button to recover it, everything looks fine.

I diffed the document.xml between the pre and post recovered versions and there are some differences, but nothing that looks wrong necessarily. I tried replacing each different element in turn manually and never got the message to go away.

I noticed that I was getting the same thing with the anchored images before but I just assumed I wasn't setting all the required properties.

When adding an inline image on a blank template or a document.New(), I don't get any errors.

Here is the code I am using after adding just a few title elements (Note that I am document.Open("template.docx") which is a file that includes a header and footer on the first page and some styles):

img, err := common.ImageFromFile("logo_client.png")
iref, err := doc.AddImage(img)
if err != nil {
	log.Fatalf("unable to add image to document: %v", err)
}

para := doc.AddParagraph()
para.Properties().SetSpacing(42*measurement.Point, 0*measurement.Point)
para.Properties().SetAlignment(wml.ST_JcCenter)
inline, err := para.AddRun().AddDrawingInline(iref) // inline, err :=
if err != nil {
	log.Fatalf("error inserting inline image %v: %v", inline, err)
}

Like I said, the document recovers fine so its not a big issue, but thought it might be worth reporting.

from unioffice.

tbaliance avatar tbaliance commented on August 29, 2024

That means we’re doing something wrong. I tested on OSX Word which is usually the most particular regarding format. What version of word reports an error?

from unioffice.

freb avatar freb commented on August 29, 2024

Microsoft Office 365, Version 1710 (Build 8625.2139), on Windows. Should be the most up-to-date version of 365.

I just reproduced the "can't open" issue with a brand new blank word document. For some reason, when I create a new word document the default is "compatibility" mode, but I get the same result whether I leave it in compatibility mode or upgrade it to the "newest file format". It doesn't give any indication as to what the actual format is before or after, but its probably in the .xml somewhere.

I uploaded the before and after recovery files (these show compatibility mode when open in Word, I did not upgrade them). I also attached the image I'm using and here is the code I ran:

package main

import (
	"log"

	"baliance.com/gooxml/document"
	"baliance.com/gooxml/common"
)

func main() {
	doc, err := document.Open("testimg.docx")
	if err != nil {
		log.Fatalf("error opening Windows Word 2016 document: %s", err)
	}

	img, err := common.ImageFromFile("test.png")
	iref, err := doc.AddImage(img)
	if err != nil {
		log.Fatalf("unable to add image to document: %v", err)
	}

	para := doc.AddParagraph()
	inline, err := para.AddRun().AddDrawingInline(iref)
	if err != nil {
		log.Fatalf("error inserting inline image %v: %v", inline, err)
	}

	doc.SaveToFile("out.docx")
	return
}

Also, I do get some skipping unspported element messages when running it against my real template, not this empty document. But they don't seem to affect anything as without adding the image it produces valid output even with the messages:

2017/12/10 21:16:11 skipping unsupported element on WdAnchor {http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing sizeRelH}
2017/12/10 21:16:11 skipping unsupported element on WdAnchor {http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing sizeRelV}
2017/12/10 21:16:11 skipping unsupported element on WdAnchor {http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing sizeRelH}
2017/12/10 21:16:11 skipping unsupported element on WdAnchor {http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing sizeRelV}
2017/12/10 21:16:11 skipping unsupported element on WdAnchor {http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing sizeRelH}
2017/12/10 21:16:11 skipping unsupported element on WdAnchor {http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing sizeRelV}

from unioffice.

tbaliance avatar tbaliance commented on August 29, 2024

Found the issue and re-submitted as #142

from unioffice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.