Hello,
I have dsoframer control with the MSWord document in it on a Windows Form. The Document that is open in dsoframer is basically of type Word.Document as declared below.
Private mwdDocument As Word.Document
...Open file from disk or database...
mwdDocument.SaveAs(lsDocName.ToString)
If File.Exists(lsDocName) = True Then
Dim lobjDocument As New Aspose.Words.Document
lobjDocument = Nothing
lobjDocument = New Aspose.Words.Document(lsDocName, Aspose.Words.LoadFormat.Doc, "")
lobjDocument.FirstSection.PageSetup.PaperSize = Aspose.Words.PaperSize.Legal*
End If
I need to get this active document which is open in dsoframer control or on mwdDocument to Aspose.Words object and work on the object to get it saved as Images. Currently I am saving the doucment to temp folder and loading it back to Aspose object. This works fine but I have to do this additional save of document(mwdDocument.SaveAs)
Is there any way to get this document out of dsoframer or from mwdDocument to Aspose.Words object without saving the document? Something like get ActiveDocument to Aspose.
Please update if you have any thoughts.
Thanks,
Vinay Hattarki
Hi
Thanks for your request. No, there is no way to create the Aspose.Words.Document directly from Word.Docuemnt object. You should save the document and then open it using Aspose.Words.
Best regards.
Thanks!!!
Is there any way I can get all the data programatically from current open Word.Document object or from DSOframer control to Aspose.Words object.
I just need to get everything from Word.Document to Aspose.Words object including formatting, font, size, alignment etc.
Any way to do this?
Thanks,
Vinay Hattarki
Hi
Thanks for your request. I think you can try using the following code:
Dim Wrd As Word.Application = New Word.Application()
'Open document
Wrd.Documents.Open("C:\Temp\in.doc")
'Get XML of the document (This is actualy WordML document)
Dim docXML As String = Wrd.ActiveDocument.Range.XML
'Do not forget to close the docuemnt
Wrd.ActiveDocument.Close()
'Now we should put this XML into the MemoryStream
'Convert string to byte array
Dim docBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(docXML)
'Create MemoryStream from byte array
Dim docStream As IO.MemoryStream = New IO.MemoryStream(docBytes)
'Now we can Open Word document from stream
Dim doc As Aspose.Words.Document = New Aspose.Words.Document(docStream)
'Change something in the document using Aspose.Words and save it
Dim builder As Aspose.Words.DocumentBuilder = New Aspose.Words.DocumentBuilder(doc)
builder.MoveToDocumentEnd()
builder.Write("Hello from Aspose.Words!")
'Save document
doc.Save("C:\Temp\out.doc")
Hope this helps.
Best regards.
Thanks!!!
I streamed and saved the document but it seems there is differnce between how it generates the TIF out of it. I have attached sample doc and two different TIF images.
If I try to create TIF out of document saved locally everything seems to be correct on border and footnote alignments, vertical spacing etc. But when I generate TIF out of streamed Aspose object the Borders are lost and also the vertical spacing is missing and Footer is carried in center of the document.
Please look at the sample doc and images. Here is the code I run… please let me know why I cannot carry the border and other properties similar way to saved document.
Dim lObjWrdApp As Word.Application
lObjWrdApp = DirectCast(Me.AxWordControl.ActiveDocument.Application, Word.Application)
If mwdDocument.ProtectionType <> Word.WdProtectionType.wdNoProtection Then
mwdDocument.Unprotect()
End If
Dim docXML As String = lObjWrdApp.ActiveDocument.Range.XML
Dim docBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(docXML)
Dim docStream As IO.MemoryStream = New IO.MemoryStream(docBytes)
Dim lobjDocument As Aspose.Words.Document = Nothing
lobjDocument = New Aspose.Words.Document(docStream)
lobjDocument.SaveToImage(liPageNo, 1, lsTifName, lobjImageOption)
Also I did upgrade to new version of aspose as but it still carried over 5 square boxes on both the images as you see. I am not sure why it is doing that.
Thanks,
Vinay Hattarki
Hi
Thanks for your request. I cannot reproduce the problem on my side. I use the latest version of Aspose.Words (6.2.0) and the following code for testing:
'Set Aspose.Words license
Dim lic As Aspose.Words.License = New Aspose.Words.License()
lic.SetLicense("Aspose.Words.lic")
'Create instance of Word.Application
Dim Wrd As Word.Application = New Word.Application()
'Open document
Wrd.Documents.Open("C:\Temp\SampleDoc.doc")
'Unprotect the document, if it is protected.
'This is needed because in case of protected document Range.XML returns empty document
If Wrd.ActiveDocument.ProtectionType <> Word.WdProtectionType.wdNoProtection Then
Wrd.ActiveDocument.Unprotect()
End If
'Get XML of the document (This is actualy WordML document)
Dim docXML As String = Wrd.ActiveDocument.Range.XML
'Do not forget to close the docuemnt
Wrd.ActiveDocument.Close()
'Now we should put this XML into the MemoryStream
'Convert string to byte array
Dim docBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(docXML)
'Create MemoryStream from byte array
Dim docStream As IO.MemoryStream = New IO.MemoryStream(docBytes)
'Now we can Open Word document from stream
Dim doc As Aspose.Words.Document = New Aspose.Words.Document(docStream)
'Save document
Dim opt As Aspose.Words.Rendering.ImageOptions = New Aspose.Words.Rendering.ImageOptions()
opt.TiffCompression = Aspose.Words.Rendering.TiffCompression.Lzw
doc.SaveToImage(0, doc.PageCount, "C:\Temp\wml.tif", opt)
'Open original DOC file and save it as TIF
Dim docSrc As Aspose.Words.Document = New Aspose.Words.Document("C:\Temp\SampleDoc.doc")
'Save document
docSrc.SaveToImage(0, docSrc.PageCount, "C:\Temp\doc.tif", opt)
I attached my results.
Best regards.
Hello,
Even with 6.2 I am getting same results. I will actually have to wait to use this stream based code usage until all the issues are fixed related to thread. <A href="
The only problem I see now is that when form fields are empty the TIF image carries 5 squares and when form field has data entered the TIF image carries correct data. It seems TIF image creates 5 squares in place of empty form field. Is there any way I can loop thru these form fields and make the NULL/Empty form fields with space =" " programatically?
This is same issue that you were not able to reproduce in TIF but you created it when saved as PDF. I still have same issue in TIF as well and need to correct it by looping thru form fields. Is thr any sample of how we can make the form fields null to space " "?
Thanks,
Vinay Hattarki
Hi
Thanks for your request. You can try using the following code:
// Open document
Document doc = new Document(@"Test232\in.doc");
// Loop through all Formfields
foreach (FormField field in doc.Range.FormFields)
{
if (string.IsNullOrEmpty(field.Result))
field.Result = string.Empty;
}
// Save document
doc.Save(@"Test232\out.doc");
Hope this helps.
Best regards.