Paste special option

Dear Team,
Is it possible to cut a image in a doc and save it in another document using paste special option as jpeg / png image.
If yes, could you please provide a workaround solution…
Attached sample:
Sample.zip (2.4 MB)

Thanks.

@resh05,

You can build logic on the following code to get the desired output:

Document doc = new Document("E:\\temp\\Sample\\2019-02-09T10-56-51_581Z_egi10S6HM1X03F.docx");

// create a new document
Document doc1 = new Document();
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.Writeln("Another new document");

// Get the shape that you want to copy
Shape targetShape = (Shape) doc.FirstSection.Body.FirstParagraph.FirstChild;
            
// import th node inside new document
Shape importedShape = (Shape)doc1.ImportNode(targetShape, true);

// insert it at the end of new document
builder.MoveToDocumentEnd();
builder.InsertNode(importedShape);

doc.Save("E:\\temp\\Sample\\19.10.docx");

Hi @awais.hafeez
Can we cut and paste an image in doc using java?
Thanks…

@resh05,

The following Aspose.Words code should simulate the desired cut and paste behavior:

Document doc = new Document("E:\\temp\\Sample\\2019-02-09T10-56-51_581Z_egi10S6HM1X03F.docx");

// create a new document
Document doc1 = new Document();
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.writeln("Another new document");

// Get the shape that you want to copy
Shape targetShape = (Shape)doc.getFirstSection().getBody().getFirstParagraph().getFirstChild();

// import the node inside new document
Shape importedShape = (Shape)doc1.importNode(targetShape, true);

// insert it at the end of new document
builder.moveToDocumentEnd();
builder.insertNode(importedShape);

// just remove the original shape in source document
targetShape.remove();
// save source document
doc.save("E:\\temp\\Sample\\2019-02-09T10-56-51_581Z_egi10S6HM1X03F.docx");

// save other document
doc1.save("E:\\temp\\Sample\\19.10.docx");

Hi @awais.hafeez

ClassCastException occurs while running the above code. Attached screenshot
Screesnhot.JPG (71.3 KB)
Thanks…

@resh05,

First off, please refer to the following section of documentation to learn the document object model of Aspose.Words:

The same code from my previous post works fine when executing against this Sample.zip (2.4 MB) document. If your are testing it against any other document, then you will have to modify this code according to the document structure of other documents.

Hi @awais.hafeez

I found the issue was from my end…The code works fine…

Thank you

@resh05,

Thanks for your feedback. It is great that you were able to resolve this issue on your end. Please let us know any time you have any further queries.

Hi @awais.hafeez
Can we paste the image as a jpeg image… ? would you please share me a workaround for this case…
Thanks…

@resh05,

Please try the following code that converts the Shape to JPEG before inserting the JPEG image to another Word document. Hope, this helps.

Document doc = new Document("E:\\temp\\Sample\\2019-02-09T10-56-51_581Z_egi10S6HM1X03F.docx");

// create a new document
Document doc1 = new Document();
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.writeln("Another new document");

// Get the shape that you want to copy
Shape targetShape = (Shape)doc.getFirstSection().getBody().getFirstParagraph().getFirstChild();

// import the node inside new document
Shape importedShape = (Shape)doc1.importNode(targetShape, true);

// convert the shape to JPEG
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.JPEG);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ShapeRenderer renderer = importedShape.getShapeRenderer();
renderer.save(baos, opts);

// insert it at the end of new document
builder.moveToDocumentEnd();
builder.insertImage(baos.toByteArray());

// just remove the original shape in source document
targetShape.remove();
// save source document
doc.save("E:\\temp\\Sample\\2019-02-09T10-56-51_581Z_egi10S6HM1X03F.docx");

// save other document
doc1.save("E:\\temp\\Sample\\19.10.docx");

Hi @awais.hafeez
The code works fine as expected for certain files…
but an error still occurs for some files…

Attached Samples:
Issue.zip (6.8 MB)
Thanks…

@resh05,

The “Issue.zip” conatins ten Word documents. Do you see errors in all of these documents? Can you please elaborate the nature of errors that you are observing on your end?

Hi @awais.hafeez

The Samples above contains the original doc and the doc that converts to jpeg Word
document. Attached screenshots of the sample documents that contains the error while it gets converted to jpeg document.

Screenshot:
Sample 2.jpg (45.4 KB)
Sample 3.jpg (59.7 KB)
Sample 23.jpg (102.0 KB)
sample 24.jpg (87.1 KB)
Sample 25.jpg (101.6 KB)

Thanks…

@resh05,

Please also provide detailed comparison screenshots highlighting all the problematic areas in Aspose.Words generated JPG files and attached them here for further testing. Thanks for your cooperation.

Hi @awais.hafeez

I have already attached screenshots in above post. Attached the screenshots again for your reference.
Screenshot:
Screenshots…zip (272.4 KB)

@resh05,

Regarding 2019-09-09T00-51-56_968Z_egi1024F6ZHK69,docx in Sample25 folder, you can fix this by copying all Shapes from one document into another. Please check the following code:

Document doc = new Document("E:\\temp\\Issue\\Sample25\\2019-09-09T00-51-56_968Z_egi1024F6ZHK69.docx");

// create a new document
Document doc1 = new Document();
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.writeln("Another new document");

// Get all the shapes that you want to copy
for (Shape targetShape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {
// import the node inside new document
    Shape importedShape = (Shape) doc1.importNode(targetShape, true);

// convert the shape to JPEG
    ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.JPEG);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ShapeRenderer renderer = importedShape.getShapeRenderer();
    renderer.save(baos, opts);

// insert it at the end of new document
    builder.moveToDocumentEnd();
    builder.insertImage(baos.toByteArray());

// just remove the original shape in source document
    targetShape.remove();
}

// save source document
doc.save("E:\\temp\\Issue\\Sample25\\2019-09-09T00-51-56_968Z_egi1024F6ZHK69.docx");

// save other document
doc1.save("E:\\temp\\Issue\\Sample25\\19.10.docx"); 

The above code will also fix the issue with ‘2019-09-10T08-17-10_542Z_egi10Q2LRDRSLZ.docx’ inside Sample24 folder.

The above code will also fix the issue with ‘2019-09-03T02-25-15_323Z_egi10QV8JQJQHJ.docx’ inside Sample23 folder.

We are also checing further the issues with documents contained inside Sample2 and Sample3 folders and will get back to you soon.

Hi @awais.hafeez
The code works absolutely fine for the the inputs:

  • 2019-09-09T00-51-56_968Z_egi1024F6ZHK69,docx in Sample25 folder

  • ‘2019-09-10T08-17-10_542Z_egi10Q2LRDRSLZ.docx’ inside Sample24 folder.

  • 2019-09-03T02-25-15_323Z_egi10QV8JQJQHJ.docx’ inside Sample23 folder…

Waiting for the solution eagerly…
Thanks…

@resh05,

We tested the scenarios and have managed to reproduce the same problems on our end. For the sake of corrections, we have logged the following issues in our issue tracking system.

WORDSNET-19442: related to 2019-06-03T15-39-29_498Z_egi103Q8ZMBVSQ.docx in Sample2 folder
WORDSNET-19443: related to 2019-06-03T15-39-29_518Z_egi101636N50KR.docx in Sample3 folder

We will further look into the details of these issues and will keep you updated on the status of corrections. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-19442) have been fixed in this Aspose.Words for .NET 21.5 update and this Aspose.Words for Java 21.5 update.

The issues you have found earlier (filed as WORDSNET-19443) have been fixed in this Aspose.Words for Java 21.9 update.