incubator-odf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Devin Han <devin...@apache.org>
Subject Re: Sizing a Image
Date Tue, 06 Dec 2011 10:03:26 GMT
2011/12/5 ikeamanual <ikeamanual@gmail.com>

> Hi Devin,
>
> Sure, here is my little test case.
> I don't know if I can attach files to the mailing list, let me know...
>
> Generate.java creates a odt file using Simple, and Read.java reads it
> using ODFDOM.
>
> After generating and opening in LibreOffice the picture is stretched
> vertically and overflows to the right.
> If you manually insert the picture in the next Cell: "Insert > Picture >
> From File ( Link == true )" , then the picture is sized correctly and an
> external link is used.
>


I test your code from my platform, it works well and the cell resize
automatically as the inserted image.  Actullay, you can see the source code
of Cell.setImage(). It has the following code to do it:
                 FrameRectangle rect = newImage.getRectangle();
                double height = rect.getHeight();
                double width = rect.getWidth();
                long widthInMI = new Double(width / 100).longValue();
                Column column = getTableColumn();
                if (widthInMI > column.getWidth()) {
                    column.setWidth(widthInMI);
                }

                long heightInMI = new Double(height / 100).longValue();
                Row row = getTableRow();
                if (heightInMI > row.getHeight()) {
                    row.setHeight(heightInMI, false);
                }
                return newImage;

It‘s strange why it doesn't work on you machine...


> I would like to generate an odt file with pictures of type "external link"
> and retrieve the link while reading.
>


Simple API doesn't support  "external link" image now,  but you can create
an requirement here:https://issues.apache.org/jira/browse/ODFTOOLKIT. Then
we will consider to realize it in the future version.

Now, you can use the following code as a temporary solution.

    //insert an "external link" image
    private static void insertExternalLinkedImage(Cell cell, String path)
            throws URISyntaxException, FileNotFoundException, IOException {
        URI uri = new URI(AnyURI.encodePath(path).toString());
        TableTableCellElementBase mCellElement = cell.getOdfElement();
        OdfFileDom dom = (OdfFileDom) mCellElement.getOwnerDocument();
        TextPElement pElement = dom.newOdfElement(TextPElement.class);
        mCellElement.appendChild(pElement);

        DrawFrameElement fElement =
dom.newOdfElement(DrawFrameElement.class);
        // A draw:image is always embedded in a draw:frame
        InputStream is = new FileInputStream(uri.getPath());
        BufferedImage image = ImageIO.read(is);
        int height = image.getHeight(null);
        int width = image.getWidth(null);
        is.close();

fElement.setSvgHeightAttribute(Length.mapToUnit(String.valueOf(height) +
"px", Unit.CENTIMETER));

fElement.setSvgWidthAttribute(Length.mapToUnit(String.valueOf(width) +
"px", Unit.CENTIMETER));
        pElement.appendChild(fElement);

        DrawImageElement imageElement = fElement.newDrawImageElement();

imageElement.setXlinkHrefAttribute("../"+AnyURI.decodePath(uri.toString()));

imageElement.setXlinkTypeAttribute(XlinkTypeAttribute.DEFAULT_VALUE);

imageElement.setXlinkShowAttribute(XlinkShowAttribute.DEFAULT_VALUE_EMBED);

imageElement.setXlinkActuateAttribute(XlinkActuateAttribute.DEFAULT_VALUE_ONLOAD);

        long widthInMI = new Double(width / 100).longValue();
        Column column = cell.getTableColumn();
        if (widthInMI > column.getWidth()) {
            column.setWidth(widthInMI);
        }

        long heightInMI = new Double(height / 100).longValue();
        Row row = cell.getTableRow();
        if (heightInMI > row.getHeight()) {
            row.setHeight(heightInMI, false);
        }
    }


> thanks,
> Jan
>
>
> On Mon, Dec 5, 2011 at 4:33 AM, Devin Han <devinhan@apache.org> wrote:
>
>> Hi,
>>
>> Simple API supports automatically sizes the image to stay inside the cell,
>> no need insert frame by yourself.
>> Could you share me you test case? Maybe there are some differences.
>>
>> 2011/12/5 ikeamanual <ikeamanual@gmail.com>
>>
>> > Hi,
>> >
>> > I am using Cell.setImage() and the image is larger then the cell, it
>> > overflows the cell boundary.
>> >
>> > I have a table with 2 columns. If I do the same in LibreOffice is
>> > automatically sizes the image to stay inside the left column.
>> >
>> > Do I somehow need to insert a Frame into the Cell?
>> >
>> > thanks,
>> > Jan
>> >
>>
>>
>>
>> --
>> -Devin
>>
>
>


-- 
-Devin

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message