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

Free Support Forum - aspose.com

Reading a Word Doc and writing into another doc

Hi there,

I am using .Net 2008 & Aspose File Version : 4.4.0.0

I have one word document. It has few styles ( tables, colors, etc.) and some bookmarks.

I want to read that doc as template and go through each bookmark, replace bookmark with text and append that whole file to one blank word Document.

At the end I will see my template multiple time in one blank file. each must be saperated by Page break ( I don't know how to put page break ).

What I am able to do now :

I am able to read template, replace bookmark with my value and save it to another word document multiple time.

What I am unable to do now:

when I read doc and write in another blank documnet, I am loosing my tables and styles.

Can you please provide me some sample code, so I can do this easily

Code:

Public Sub GenerateDocument()

Dim oService As cService

Dim builder As DocumentBuilder

dstDoc = New Document(Path.Combine(Me.TemplateDir, MASTER_TEMPLATE_DOC_NAME))

builder = New DocumentBuilder(dstDoc)

oService = New cService()

oService.GetServiceByServiceTypeID()

For Each aSerivce In oService.oDocSummaryServices

srcDoc = New Document(Path.Combine(Me.TemplateDir, SERVICE_TEMPLATE_DOC_NAME))

FillServiceTemplate(aSerivce, srcDoc)

builder.Write(srcDoc.Document.ToTxt)

builder.Writeln()

Next

dstDoc.Save(Path.Combine(Me.TemplateDir, "test.doc"), SaveFormat.Doc)

End Sub

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, you can easily achieve what you need using simple mail merge:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/simple-mail-merge-explained.html

In this case, template will be repeated automatically for each record in your datasource.

Also, you mentioned that tables and styles are lost after open/save your document using Aspose.Words. Could you please attach your document here for testing? I will check this problem and provide you more information.

Best regards,

Hi there,

Following is my code. I am trying to generate MS Word and PDF document after following things. I will really if you can help me fixing this problem as this is going in circle since last few days.

  • Reading values from DB
  • Merging to one or more document
  • Final output will be multipage document.

This is a function from my business object, This is generating document via Aspose and returns Byte array and UI throws that Byte array to response object.

I was getting this when I used old version of ASPOSE, so, I have asked my company to upgrade with your new version, but no difference.

It's working Great with MS Word. So, Rest of the bits are correct. except I can't generate PDF. I have multiple tables and text, no images.

See Attached image when I generate PDF doc. Image shows error. I am not doing anything with email.

Interesting thing:

I saw your website for example and I found that you guys are saving first as a XML file and then saving as PDF. I did same and it worked fine. Means, I have generated PDF in C:\temp\abc.pdf. But, So, ASPOSE PDF product doesn't mind my word document manipulation.

But, it's something to do with Memory stream. When I use memory stream, it doesn't work well.

  • I have tried SaveFormat.AsposePdf & SaveFormat.PDF
  • aSaveFormat is my variable where I am passing either SaveFormat.AsposePdf or SaveFormat.Doc
  • oDocSummaryServices is used for some data manipulation

Public Function ExportView(ByVal aSaveFormat As SaveFormat, ByVal oDocSummaryServices As List(Of NEMICSDO.DocSummaryService), ByRef SizeOfFile As Integer) As Byte()

Try

Me.dstDoc = New Document(Path.Combine(Me.sTemplateDir, Me.DOC_MasterPage_Template))

Me.m_oMemoryStream = New IO.MemoryStream

' Doing Document manipulation

For Each aSerivce In oDocSummaryServices

Me.srcDoc = New Document(Path.Combine(Me.sTemplateDir, Me.sTemplateFile))

Dim srcStream As New IO.MemoryStream

Me.srcDoc.Save(srcStream, SaveFormat.Doc)

FillServiceTemplate(aSerivce, Me.srcDoc)

CType(Me.srcDoc.ChildNodes(0), Aspose.Words.Section).PageSetup.SectionStart = SectionStart.Continuous

Appendchild()

Next

' At this point, full document is ready as client expects.

'Saving Document

Me.dstDoc.Save(Me.m_oMemoryStream, aSaveFormat)

If aSaveFormat = SaveFormat.AsposePdf Then

' If we want PDF

Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()

pdf.BindXML(Me.m_oMemoryStream, Nothing)

pdf.Save(Me.m_oMemoryStream)

End If

SizeOfFile = Me.m_oMemoryStream.Length

Return ConveryMemoryStreamToBytes()

Catch ex As Exception

Throw

End Try

Return Nothing

End Function

Private Function ConveryMemoryStreamToBytes() As Byte()

Try

'Seek to the beginning so it can be read by XmlDocument.

Me.m_oMemoryStream.Seek(0, SeekOrigin.Begin)

Dim oData(Me.m_oMemoryStream.Length) As Byte

Me.m_oMemoryStream.Read(oData, 0, Me.m_oMemoryStream.Length)

Return oData

Catch ex As Exception

Throw

End Try

End Function

On UI

Dim ByteDoc As Byte() = oDocExport.ExportView(ExportType, _DocSummaryServices, SizeOfFile)

Try

If ExportType = Constants.ExportType.PDF Then

Response.ContentType = "application/pdf"

Response.Clear()

Response.ClearHeaders()

Response.ClearContent()

Response.AddHeader("Content-Disposition", "attachment;filename=" + "Services_" & Now.Date.ToShortDateString & ".pdf")

ElseIf ExportType = Constants.ExportType.MSWord Then

Response.ContentType = "application/msword"

Response.Clear()

Response.ClearHeaders()

Response.ClearContent()

Response.AddHeader("Content-Disposition", "attachment;filename=" + "Services_" & Now.Date.ToShortDateString & ".doc")

End If

Response.OutputStream.Write(ByteDoc, 0, SizeOfFile)

Catch ex As Exception

Throw

End Try

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. With the latest version of Aspose.Words you do not need to save your document into intermediate XML to convert it to PDF. You can save directly to PDF using Aspose.Words:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-convert-a-document-to-pdf.html

Also, if you need to send document into client’s browser, you can try using the following simple code:

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

Best regards,

Hi there,

If you read my query carefully, I am already saying that when I save it C:\temp\abc.pdf, it works fine. My requirement is not to save it on server.

My requirement is to create proper memory stream and return it to caller and that caller will send that response to user browser.

Please see my code and let me know if something is wrong there. Or any other code which deals with memory stream.

Thanks. Waiting for your reply.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can use the following simple code to save document as PDF to stream:

// Open source docuemnt from file or stream

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

// Create MemoryStrema where we will save output PDF.

MemoryStream pdfStream = new MemoryStream();

// Save output document as PDF to stream.

doc.Save(pdfStream, SaveFormat.Pdf);

Best regards,

Hi axeley,

We have license copy of Aspose and I believe we should be able to get priority support as required. Thanks for your response. However, What I am asking, and what I am getting is not same.

If you re-read my previous 2 posts, I am saying I don't have any problem reading DOC, Creating doc, saving doc. I have problem, when I use memory stream to generate PDF document. Please see my code. Same code creates WORD document without any problem.

I believe that's bug in the product, however, I am not sure.

It will be great if you try to use my code and see what's the error when we use memory stream.

Thanks.

Ankit

Hi Ankit,

Thanks for this additonal information.

From the description of the symptoms it seems most likely that the issue with the PDF becoming corrupt is occuring with the Aspose.Pdf component and not with Aspose.Words. You can make sure of this by trying direct PDF conversion as Alexey suggested above and seeing if the issue dissapears.

I have done a quick test using this method and the basis of your code appeared to work with the direct PDF method. Please note that saving to the PDF using Aspose.Pdf is a legacy method and will be removed in the next version. For improved functionality we suggest you use the direct rendering method (through the Save method) instead.

Also as Alexey stated your could simply alot of your code by using the save overload to send the document to browser

For your reference here is the code posted above to save a document to a stream using the direct conversion method to PDF in Visual Basic.

Dim doc As New Document("C:\Temp\in.doc")

Dim pdfStream As New MemoryStream()
doc.Save(pdfStream, SaveFormat.Pdf)

You can send the document to the browser in one line of code using the overload below

doc.Save(Response, "Services_" & Now.Date.ToShortDateString & ".pdf", ContentDisposition.Inline, Nothing)

If you have any further issues after trying these suggestions please feel free to ask for further help.

Thanks,

Memory Stream is always problem, where I use SaveFormat.AsposePDF

However, it works if I use little trick with SaveFormat.PDF. So, my issue is being fixed and I can generate similar document as I can generate through MS Word option.