OLE image is half cut

Hi Team,

When I include a OLE into a word document, I could see ONLY part of the image from the OLE in the front. Once I double click, I could see full image. Is there are a way to set it to show up as much of image as per available page width?

See attached code and document. In the attached document, even though there is enough width available, the OLE is not seen completely. Instead, it is half cut.

You could used the attached doc as the rtf file to be included as physical embedding.

Thanks,
Kumar

public class TestIncludeOLE
{
    public static void main(String[] args) throws Exception
    {
        TestAsposeUtils.setupLicense(TestAsposeUtils.lICENSE_FILE_PATH);
        Document doc = new Document();
        DocumentBuilder docBuilder = new DocumentBuilder(doc);

        docBuilder.getCurrentSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);

        String includeDcoPath = "D:\\OLE_505c332a17a813b6_000010532Object_Text_0.rtf";

        insertOLEDocument(new Document(includeDcoPath), docBuilder, doc);

        doc.updateFields();

        String unique = UUID.randomUUID().toString().replaceAll(" ", "_");
        String docpath = System.getProperty("java.io.tmpdir") + "test_" + unique + "_" + BuildVersionInfo.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$
        doc.save(docpath + ".doc"); //$NON-NLS-1$
        System.out.println(docpath + ".doc");
    }

    private static void insertOLEDocument(Document srcDoc, DocumentBuilder docBuilder, Document doc) throws Exception
    {
        // This object will be translating styles and lists during the import.
        NodeImporter importer = new NodeImporter(srcDoc, doc, ImportFormatMode.USE_DESTINATION_STYLES);

        // Node lastNode = docBuilder.getCurrentParagraph();
        CompositeNode<?> dstStory = docBuilder.getCurrentParagraph();
        Node lastNode = dstStory.getLastChild();

        for (Section srcSection : srcDoc.getSections())
        {
            for (Node srcNode : srcSection.getBody())
            {
                NodeCollection shapes = ((CompositeNode<?>) srcNode).getChildNodes(NodeType.SHAPE, true);
                for (Shape shape : shapes)
                {
                    Node newNode = importer.importNode(shape, true);

                    try
                    {
                        Node importedNode = dstStory.insertAfter(newNode, lastNode);
                        lastNode = importedNode;
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}

Hi Kumar,

Thanks for your inquiry. Please use following method to resize the shape. Hope this helps you.

If you still face problem, please attach your input RTF (OLE_505c332a17a813b6_000010532Object_Text_0.rtf) document here for testing. I will investigate the issue on my side and provide you more information.

public static void resizeLargeImage(Shape image) throws Exception
{
    // Return if this shape is not an image.
    if (!image.hasImage())
        return;
    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.getParentParagraph().getParentSection().getPageSetup();
    double freePageWidth = image.isInline() ? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin() : ps.getPageWidth();
    double freePageHeight = image.isInline() ? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin() : ps.getPageHeight();
    ImageSize size = image.getImageData().getImageSize();
    boolean exceedsMaxPageSize = size.getWidthPoints() > freePageWidth || size.getHeightPoints() > freePageHeight;
    if (exceedsMaxPageSize)
    {
        // Calculate the ratio to fit the page size based on which side is longer.
        boolean widthLonger = (size.getWidthPoints() > size.getHeightPoints());
        double ratio = widthLonger ? freePageWidth / size.getWidthPoints() : freePageHeight / size.getHeightPoints();
        // Set the new size.
        image.setWidth(size.getWidthPoints()  ratio);
        image.setHeight(size.getHeightPoints()  ratio);
    }
}

Hi Tahir,

Probably I did not do a good job explaining the problem.

If the OLE file is included in Word document, within the document it is the shown like a preview. When I double click on it, a new document is opened containing the OLE, where I could edit it and save back to original document.

The question is, why is the preview half cut even though there is enough space to its right?

See attached screenshot.

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. It would be great if you please share your Rtf (OLE_505c332a17a813b6_000010532Object_Text_0.rtf) here for testing purposes. Unfortunately,
it is difficult to say what the problem is without the Document(s). Once we have your document, we’ll start our investigation into your issue.

As mentioned in my first thread, please use the attached document in first thread as the file to be included.

Hi Kumar,

Thanks for sharing the detail. Please note that Aspose.Words mimics the same behavior as MS Word does. You are inserting the embedded object from your shared document into new empty document. If you do the same scenario using MS Word, you will get the same output.

*kumaraswamy.m:

The question is, why is the preview half cut even though there is enough space to its right?*

The shared document have an embedded Word document. Press Alt + F9 to see the field codes (EMBED Word.Document.12). You are facing the expected behaviors of MS Word. Please do the following steps to check this behavior.

  1. Open your document and press double click on embedded object.
  2. The embedded Word document is opened separately. Save as this document e.g embedded.docx
  3. Open a new empty document
  4. Insert embedded.docx as an embedded object from Insert tab into this new empty document. You will observe that the preview of embedded object is not complete.

Hope this answers your query. Please let us know if you have any more queries.

Hi Tahir,

I too observed the same behavior in MS Word. But is there a way to see full image (or upto available page width) of the preview of embedded document?

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. You are facing the expected behavior of Aspose.Words. In your case, I suggest you following solution.

  1. Extract the OLE object (docx) from the source document
  2. Pass this document to insertOLEDocument method.

Please check following code example for your kind reference. Hope this helps you.

Document srcDoc = new Document(MyDir + "test_80eabb22-9377-40aa-9250-8cedb64ff0d5_15.6.0.0.doc");
Shape shape = (Shape)srcDoc.getChild(NodeType.SHAPE, 0, true);
if (shape.getOleFormat() != null
&& shape.getOleFormat().getProgId().trim().equals("Word.Document.12"))
{
    shape.getOleFormat().save(MyDir + "Ole.docx");
}
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.getCurrentSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);
docBuilder.getCurrentSection().getPageSetup().setPageWidth(1250);
String includeDcoPath = MyDir + "Ole.docx";
insertOLEDocument(new Document(includeDcoPath), docBuilder, doc);
doc.save(MyDir + "Out.docx");