We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Converting a Text stream to docx

Hi,
I want to convert a text stream to docx. Can you please tell me how I will achieve this??

CODE:

Document doc = new Document(getPathToTemplateDoc());
System.out.println(“Template fields ::”+doc.getMailMerge().getFieldNames());

NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
System.out.println(shapes.getCount());
for (Shape shape : (Iterable) shapes)
if(shape!=null){
if (shape.hasImage())
{
shape.remove();
}
}


ByteArrayOutputStream dstStream = new ByteArrayOutputStream();

doc.save(dstStream, SaveFormat.TEXT);

ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());
Document docx = new Document(srcStream);

return docx;

Hello

Thanks for your request. You should use the code like the following:

ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());

Document docx = new Document();

DocumentBuilder builder = new DocumentBuilder(docx);

try

{

// You might need to specify a different encoding depending on your plain text files.

BufferedReader reader = new BufferedReader(new InputStreamReader(srcStream, "UTF8"));

String line = null;

// Read plain text "lines" and convert them into paragraphs in the document.

while ((line = reader.readLine()) != null)

{

builder.writeln(line);

}

reader.close();

}

finally { if (srcStream != null) srcStream.close(); }

docx.save("C:\\Temp\\out.docx", SaveFormat.DOCX);

Best regards,

Thanks for your reply,
One more problem is there…I am trying to remove images after loading the file and again need the images while saving it as docx… I am using following code, but all the images are not removing …its still there what to do pls suggest me? Is there some particular shape of images we can remove or all… and again how can I get those images back…
Here is the code given by Aspose

<span style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:Calibri;
mso-fareast-theme-font:minor-latin;color:#2B91AF;mso-ansi-language:RU;
mso-fareast-language:EN-US;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:Calibri;
mso-fareast-theme-font:minor-latin;mso-ansi-language:RU;mso-fareast-language:
EN-US;mso-no-proof:yes”> doc = new Document(“C:\test\in.docx”);

NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true, false);

for (int i = 0; i < shapes.getCount(); i++)

{

Shape shape = (Shape)shapes.get(i);

if (shape.hasImage())

{

shape.remove();

}

<span style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:Calibri;
mso-fareast-theme-font:minor-latin;mso-ansi-language:RU;mso-fareast-language:
EN-US;mso-no-proof:yes”>}

Thank You,

Hello

Thanks for your request. It seems there are DrawingML objects inside your DOCX document.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.drawing.drawingml.html

You can use DocumentExplorer (Aspose.Words demo application) to inspect structure of your document. This demo is included into Aspose.Words MSI installation package.

So you should remove DrawingML images too:

// Open source document

Document doc = new Document("C:\\Temp\\in.docx");

// Get collection of Shapes and remove them

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

shapes.Clear();

// Get collection of DrawingML and remove them.

NodeCollection dmls = doc.GetChildNodes(NodeType.DrawingML, true);

dmls.Clear();

// Save as HTML

doc.Save("C:\\Temp\\out.docx");

Regarding getting images back, it is not so ease as sounds. First of all you need to mark the current position of a particular image for example by using Bookmarks [image#1]. Then you can try looping through the original document with images and import Images to current document using NodeImporter for example:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.nodeimporter.html

Best regards,