poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ramin Assisi <ramin.assis...@googlemail.com>
Subject Re: XWPFDocument and addPicture seems not to work
Date Wed, 15 Dec 2010 17:31:33 GMT
Hi Dave,
we use here the POI-jar without modification. As POI regarding OOXML
is not complete we have added a lot of code outside in our
solution. I think that this ok and does not violate the Apache license
as we provide also the license text of Apache.
When I help people here with code anyone is free to do what ever he
wants with those snippets.
Do you see any problems with this? By the way my help was not meant as
a contribution to Apache. But of course we would
appreciate if these extension are not needed any more because they are
already provided by the POI.

Ramin


On 15 December 2010 16:40, David Fisher <dfisher@jmlafferty.com> wrote:
> Hi Ramin,
>
> If you have some solutions to your trouble that you would like to contribute to Apache
POI, please see http://poi.apache.org/guidelines.html
>
> Keep in mind that Apache POI and all contributions are made under the Apache Software
License - http://www.apache.org/licenses/
>
> Regards,
> Dave
>
> On Dec 15, 2010, at 8:20 AM, Ramin Assisi wrote:
>
>> Hi Chris,
>>
>> I had the same problem and I have pot it here a few months ago. I have
>> also reported that all the related tests do not create
>> the image resources.
>>
>> Here is my solution (from the JOffice sources). It might give you some ideas.
>>
>>
>>
>>       /**
>>        * @param path
>>        * @param imageName
>>        * @throws IOException
>>        */
>>       @Override
>>       public String addImage(String path, JOSVG svg, Rectangle imageBounds)
>>                       throws IOException {
>>               try {
>>                       File imageFile = new File(path);
>>                       if (svg == null && !imageFile.exists())
{
>>                               throw new IllegalArgumentException("file
does not exist"); //$NON-NLS-1$
>>                       }
>>                       if ("".equals(path)) //$NON-NLS-1$
>>                               throw new IllegalArgumentException("path");
//$NON-NLS-1$
>>
>>                       PackagePartName thumbnailPartName = getImagePartName(path);
>>                       PackagePart imagePart = null;
>>                       String filename = path.substring(path
>>                                       .lastIndexOf(File.separatorChar)
+ 1);
>>                       String contentType = ContentTypes
>>                                       .getContentTypeFromFileExtension(filename);
>>                       if (packagePart.getPackage().getPart(thumbnailPartName)
!= null) {
>>                               imagePart = packagePart.getPackage().getPart(thumbnailPartName);
>>                       } else {
>>                               imagePart = packagePart.getPackage().createPart(
>>                                               thumbnailPartName,
contentType, false);
>>                       }
>>
>>                       if (svg != null) {
>>                               svg.setPackagePart(imagePart);
>>                       }
>>                       MemoryPackagePartOutputStream os = (MemoryPackagePartOutputStream)
imagePart
>>                                       .getOutputStream();
>>                       if (svg == null) {
>>                               InputStream is = new FileInputStream(path);
>>                               byte[] bytes = JOUtil.copyStreamTyByteArray(is);
>>                               os.write(bytes);
>>                       } else {
>>                               svg.save(os);
>>                       }
>>                       os.flush();
>>                       os.close();
>>
>>                       PackagePart documentPart = packagePart.getPackage().getPart(
>>                                       PackagingURIHelper.createPartName("/"
+ getPrefix() //$NON-NLS-1$
>>                                                      
+ "/document.xml")); //$NON-NLS-1$
>>                       PackageRelationship rel = documentPart.addRelationship(
>>                                       thumbnailPartName, TargetMode.INTERNAL,
>>                                       PackageRelationshipTypes.IMAGE_PART);
>>
>>                       String id = rel.getId();
>>
>>                       if (JOResources.isValidImage(path)) {
>>                               imagePart.close();
>>                               Image image = JOUtil
>>                                               .createImage(new
ImageLoader().load(path)[0]);
>>                               Rectangle r = image.getBounds();
>>                               imageBounds.width = r.width;
>>                               imageBounds.height = r.height;
>>                       } else if (contentType.equals(JO.CONTENT_TYPE_IMAGE_SVG))
{
>>                               imageBounds.width = 100;
>>                               imageBounds.height = 100;
>>                       }
>>                       pathToId.put(path, id);
>>                       return id;
>>               } catch (InvalidFormatException e) {
>>                       LOGGER.error("", e); //$NON-NLS-1$
>>               }
>>
>>               return null;
>>
>>       }
>>
>>
>> Ramin
>>
>> http://joffice.eu
>>
>>
>>
>> On 15 December 2010 13:20, ChrisZimmer
>> <christian.zi.zimmer@partner.bmw.de> wrote:
>>>
>>> Hi all,
>>>
>>> i've got the following problem:
>>> I try to generate a .docx with embedded images. The generation process
>>> succeeds, but the .docx is not showing any (programmatically) added images.
>>>
>>> If i unzip the .docx, the folder media is showing all images and the
>>> "document.xml.rels" contains the generated ids for the images.
>>>
>>> If i check the docuemnt.xml none of the ids is referenced, so actually the
>>> embeding of the images is not performed by POI 3.7.
>>>
>>> I use the following code snippet to embed the images:
>>>
>>>  ...
>>>  out = new FileOutputStream("importantDoc.docx");
>>>  XWPFDocument outputDocument = new XWPFDocument(out );
>>>  ...
>>>
>>>  FileInputStream fis = new FileInputStream ( ".\\Pictures\\image01.png");
>>>  bytes[] bytes = IOUtils.toByteArray(fis);
>>>  int pdx = outputDocument.addPicture (bytes, XWPFDocument.PICTURE_TYPE_PNG);
>>>  fis.close();
>>>  ...
>>>
>>> outputDocuemnt.write(out);
>>>  ...
>>>
>>> Can anybody help me with that ? Am i missing something.
>>> Any feedback will be appreciated.
>>>
>>> Thanks in advance,
>>> Christian
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context: http://apache-poi.1045710.n5.nabble.com/XWPFDocument-and-addPicture-seems-not-to-work-tp3306236p3306236.html
>>> Sent from the POI - User mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>>> For additional commands, e-mail: user-help@poi.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message