Page 1 of 1

To import one docx (with image) into another docx

PostPosted: Fri Mar 26, 2010 12:39 am
by rockym
Hi Jason,

Thank you very much for your reply. Questions and answers are attached below. I will try it with your suggestion and let you know.

Cheers,
Rocky
======================================================

> Hi Jason,
>
> I tried to register an account on your website today. However I
> haven't received any confirmation yet. So that I have to send my
> question via email,

No sure why that might be - it happens sometimes (could be a spam filter at your end, or something wrong with the mail sender). Anyway, I've activated your registration manually. I'll answer your question here, but please post your question and my response to the forum, thanks.

> I'm using docx4j 2.3.0.
>
> I'm working on the docx importing, to import one docx file into another. Could you please advice, in general, what should be paid attention?

See

http://blogs.msdn.com/brian_jones/archi ... ingml.aspx

http://blogs.msdn.com/ericwhite/archive ... l-sdk.aspx

Or you can use altChunk, as those posts mention.

>
> Currently I have a problem to import images. Here is an example,
>
> In the main.docx, there is a picture, so that in the word\media directory there is a file image1.jpeg.
>
> In the sub.docx, there is a picture, so that in the word\media directory there is a file image1.jpeg, which is a different picture than the one in main.docx.
>
> When using BinaryPartAbstractImage.createImagePart to import sub docx into main.docx, sub image1.jpeg overrides main image1.jpeg.
>
> I'm suspecting that in docx4j the way to generate image name, with a static counter, may not be appropriate for the situation like, batch processing, multiple thread, etc. I like the way to generate Id in RelationshipsPart, methods getNextId() and resetIdAllocator(). Do you have any suggestion on this issue?
>
> Please correct me if I'm wrong, or there is a better way to create image part.

In RelationshipsPart, the method addRelationship will automatically assign an id if the rel being added does not already have one.

In this automatic assignment mode, the code needs to be changed to call resetIdAllocator() before getNextId(). I've committed this fix.

If you have to use v2.3.0, you ought to be able to achieve the same effect by calling:

documentPart.getRelationshipsPart().resetIdAllocator()

before you createImagePart

Alternatively, you can create the rel yourself, and assign it an id which does not clash. Call this yourRel.
You would then call you main document part's relationships part's loadPart(existingImagePart, yourRel) You may need to existingImagePart.setPackage( newPackage) first.
You'd also have to create the Inline image object yourself.

Hope this helps

cheers

Jason

>
> Thanks,
> Rocky
>
>
>

Re: To import one docx (with image) into another docx

PostPosted: Fri Mar 26, 2010 2:29 am
by rockym
Hi Jason,

Please let me to reorganize my question,

Here is one example,

There is a docx file with an eixsting jpeg picture inside, hint inside docx, the picture file is stored in directory word\media\image1.jpeg.

Then I modified AddImage.java to add a new jpeg picture into this docx file. The new picture was successfully added into the docx file, however the old picture was gone.

Please refer to the attached files for my testing.

I think the problem is that in class BinaryPartAbstractImage it uses a static field as image file name sequence counter.
It will cause more problem when
1. batch processing, the sequence number will always increase,
2. multi-thread processing, the concurrently processed docx files may get wrong sequence number.

Any suggestion?

Thanks,
Rocky

Re: To import one docx (with image) into another docx

PostPosted: Sun Mar 28, 2010 11:27 am
by jason
rockym wrote:I think the problem is that in class BinaryPartAbstractImage it uses a static field as image file name sequence counter.
It will cause more problem when
1. batch processing, the sequence number will always increase,
2. multi-thread processing, the concurrently processed docx files may get wrong sequence number.


Hi Rocky

I've reworked things so that BinaryPartAbstractImage derives the image file name from the relId the image part will be allocated. In other words, provided the relId is unique (which docx4j should ensure), the image part name will be unique as well (unless you manually create an image with the same part name!).

See http://dev.plutext.org/trac/docx4j/changeset/1107
(the patch is larger than strictly necessary, since I took the opportunity to re-order the methods in RelationshipsPart)

http://dev.plutext.org/docx4j/docx4j-ni ... 100328.jar incorporates this fix. It is a drop-in replacement for the v2.3.0 docx4j jar file.

cheers .. Jason

Re: To import one docx (with image) into another docx

PostPosted: Tue Mar 30, 2010 4:21 am
by reddd
Hi guys,
i have quite the same problem..
i'm trying to use the docx as a template, with an image into the header, but when i put other informations in the document, with some other images, the sistem overwrite the first image (the one in the header) with the first of the new pictures. so everytime i loose my logo of the header.

I just download your last release of the docx4j jar, but the problem seems to persist.

i hope to have been clear to explain my problem...
have you got any suggestion?

thanks ssssso much!

Stefano

Re: To import one docx (with image) into another docx

PostPosted: Tue Mar 30, 2010 7:11 am
by rockym
Hi Jason,

Thank you so much for the fix. It works fine so far.

There is one typo found in ImageGifPart.java. The highlighted part should be IMAGE_GIF.

public void init() {
// Used if this Part is added to [Content_Types].xml
setContentType(new org.docx4j.openpackaging.contenttype.ContentType(
org.docx4j.openpackaging.contenttype.ContentTypes.IMAGE_PNG));

// Used when this Part is added to a rels
setRelationshipType(Namespaces.IMAGE);
}

Thanks,
Rocky

Re: To import one docx (with image) into another docx

PostPosted: Tue Mar 30, 2010 7:17 am
by rockym
Hi reddd,

I updated the source code from HEAD and it has all changes for this issue.

Regards,
Rocky

Re: To import one docx (with image) into another docx

PostPosted: Tue Mar 30, 2010 8:45 pm
by reddd
Thank you so much ,
everything is working correctly now, i was forgotting some fundamental libraries...mea culpa!
Now i have a different problem, i have to assign style to the HTML, but i think this is not the right thread to talk about it.
thanks again everybody

Reddd

Re: To import one docx (with image) into another docx

PostPosted: Tue Mar 30, 2010 9:09 pm
by jason
reddd wrote:i have to assign style to the HTML


Yes, please start another thread if you want to discuss this. But if you are creating HTML with docx4j's NG2, it should generate appropriate styles for you....