Free Support Forum - aspose.com

Converting Interop document formating code to Aspose

Hi Aspose team,

I am trying to convert below Interop function to Aspose.

Interop code:

Public Function GetSplitText(ByRef objAction As Action, ByVal strDocName As String, ByVal ParentDoc As String) As String

Dim objSourceDoc As DocumentClass

Dim sngRightMargin As Single

Dim sngBottomMargin As Single

Dim strOverFlowDoc As String

Dim intStartOfRange As Integer

Dim wParentDoc As DocumentClass

objSourceDoc = mobjWordApp.Documents(strDocName)

wParentDoc = mobjWordApp.Documents(ParentDoc)

strOverFlowDoc = objSourceDoc.Name

mobjFormatter.TargetDocumentName = ParentDoc

mobjFormatter.SetFormatText(strDocName)

objSourceDoc.Activate()

With objSourceDoc.PageSetup

sngRightMargin = (objAction.MaxWidth - 8) * -1

sngBottomMargin = (objAction.MaxHeight - 10) * -1

.TopMargin = mobjWordApp.InchesToPoints(0)

.BottomMargin = mobjWordApp.InchesToPoints(11 - objAction.MaxHeight)

.LeftMargin = mobjWordApp.InchesToPoints(0)

.RightMargin = mobjWordApp.InchesToPoints(8.5 - objAction.MaxWidth)

.Gutter = mobjWordApp.InchesToPoints(0)

.PageWidth = mobjWordApp.InchesToPoints(8.5)

.PageHeight = mobjWordApp.InchesToPoints(11)

objSourceDoc.ActiveWindow.Selection.HomeKey(WdUnits.wdStory)

End With

With objSourceDoc.ActiveWindow.Selection

.HomeKey(WdUnits.wdStory)

intStartOfRange = .Start

'try going to page two. if there is now page two the ranges equal

.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToNext, Name:="2")

If intStartOfRange = .Start Then

strOverFlowDoc = "1" + strOverFlowDoc '1 denotes 1 page only, no overflow on to addendum

.HomeKey(WdUnits.wdStory)

.EndKey(WdUnits.wdStory, 1) 'highlight entire text

.MoveLeft(WdUnits.wdCharacter, 1, True)

Return strOverFlowDoc

End If

intStartOfRange = .Start

.MoveLeft(WdUnits.wdCharacter, 1)

.MoveUp(WdUnits.wdLine, 1)

.EndKey(WdUnits.wdLine, 0)

.InsertBreak(WdBreakType.wdPageBreak)

'move left 2, one for the page break, one for the paragraph break that putting a page break in inserts

.MoveLeft(WdUnits.wdCharacter, 2)

.InsertBreak(WdBreakType.wdLineBreak)

.TypeText("(" + objAction.OverflowEndMessage + ")")

.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToNext, Name:="2")

.EndKey(WdUnits.wdStory, 1) 'highlight entire text

.MoveLeft(WdUnits.wdCharacter, 1, True)

Return "2" + strOverFlowDoc

End With

End Function

--------------------------------------------------------------------------

My Aspose converted code:

Public Function GetSplitText_Aspose(ByRef objAction As Action, ByVal strDocName As String, ByVal ParentDoc As String) As String

Dim objSourceDoc As Aspose.Words.Document

Dim strOverFlowDoc As String

objSourceDoc = New Aspose.Words.Document(mstrWorkDirectory + strDocName)

strOverFlowDoc = strDocName

mobjFormatter.TargetDocumentName = mstrWorkDirectory + ParentDoc

mobjFormatter.SetFormatText_Aspose(mstrWorkDirectory + strDocName)

Dim objBuilder As DocumentBuilder = New DocumentBuilder(objSourceDoc)

objBuilder.PageSetup.TopMargin = ConvertUtil.InchToPoint(0)

objBuilder.PageSetup.BottomMargin = ConvertUtil.InchToPoint(11 - objAction.MaxHeight)

objBuilder.PageSetup.LeftMargin = ConvertUtil.InchToPoint(0)

objBuilder.PageSetup.RightMargin = ConvertUtil.InchToPoint(8.5 - objAction.MaxWidth)

objBuilder.PageSetup.Gutter = ConvertUtil.InchToPoint(0)

objBuilder.PageSetup.PageWidth = ConvertUtil.InchToPoint(8.5)

objBuilder.PageSetup.PageHeight = ConvertUtil.InchToPoint(11)

objSourceDoc.Save(mstrWorkDirectory + strDocName)

If objSourceDoc.PageCount > 1 Then

Dim nodes As New ArrayList()

Dim firstPagePara As Paragraph = Nothing

objBuilder.MoveTo(firstPagePara)

objBuilder.InsertBreak(BreakType.LineBreak)

objBuilder.Write("(" + objAction.OverflowEndMessage + ")")

moverflowText = objAction.OverflowEndMessage.Trim

objSourceDoc.Save(mstrWorkDirectory + strDocName)

Return "2" + strOverFlowDoc

Else

Return "1" + strOverFlowDoc

End If

End Function

I need your help to convert the highlighted Interop code to Aspose.

Thank you for your help in advance.

Thanks again,

Prajakta.

Hi Prajakta,

Thanks for your inquiry. It would be great if you please share following detail:


  • Please attach your input Word document.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document or generate the document using Interop code.

I will
investigate as to how you are expecting your final document be generated
like. We will then provide you more information on this along with code.

Hi Tahir,

Thanks a ton for your quick response.

PSA the input and output documents generated by the interop code.

I want the similar output with Aspose.

The statement : .TypeText("("+ objAction.OverflowEndMessage+")")

appends the text "(continued on attached Addendeum)"

I am having tough time to place this text at that specific location (refer the Final Output doc) in the document at end of text at first page.

Please provide your help as soon as possible.

Thank you very much for all your help provided till now. Expect the same in future.

Thanks again,

Prajakta.

Hi Prajakta,

Thanks for sharing the documents. I suggest you please read the article about find and replace from here:
http://www.aspose.com/docs/display/wordsnet/Find+and+Replace+Overview

Following code example finds the text ‘and all accounts of’ and replace it with line break, specific text and page break. If you want to insert text at specific location based on some condition, please share that detail here for our reference. We will then provide you more information on this.

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>ZH-CN</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
<w:UseFELayout/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0pt 5.4pt 0pt 5.4pt; mso-para-margin-top:0pt; mso-para-margin-right:0pt; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0pt; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

Dim objSourceDoc As Aspose.Words.Document = New Aspose.Words.Document("ch000001-Input.doc")

Dim objBuilder As DocumentBuilder = New DocumentBuilder(objSourceDoc)

objBuilder.PageSetup.TopMargin = ConvertUtil.InchToPoint(0)

objBuilder.PageSetup.BottomMargin = 741.6

objBuilder.PageSetup.LeftMargin = ConvertUtil.InchToPoint(0)

objBuilder.PageSetup.RightMargin = 72

objBuilder.PageSetup.Gutter = ConvertUtil.InchToPoint(0)

objBuilder.PageSetup.PageWidth = ConvertUtil.InchToPoint(8.5)

objBuilder.PageSetup.PageHeight = ConvertUtil.InchToPoint(11)

For Each run As Run In objSourceDoc.GetChildNodes(NodeType.Run, True)

run.Font.Size = 8

run.Font.Name = "Arial"

Next

Dim paragraph As Paragraph = DirectCast(objSourceDoc.GetChild(NodeType.Paragraph, 2, True), Paragraph)

paragraph.Range.Replace("and all accounts of", ControlChar.LineBreak + "(continued on attached Addendum)" + ControlChar.PageBreak, False, False)

objSourceDoc.Save("Out.docx")



Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi Tahir,

Thank you for your earlier reply.

The file which I shared was just example, and it tends to change with different data.

You can see the interop code in my first post, it just moves few locations to do the formating in the document.

The document and its data is going to change regularly on every different LEAD Number, so i wanted the code similar to Interop which will help me get the proper formating.

Please let me know if you need any more input from my end.

Thanks again for your kind revert,

Prajakta.

Hi Prajakta,

Thanks for your inquiry. Please note that Aspose.Words is quite
different from the Microsoft Word’s Object Model in that it represents
the document as a tree of objects more like an XML DOM tree. If
you worked with any XML DOM library you will find it is easy to
understand and work with Aspose.Words. When you load a Word document
into Aspose.Words, it builds its DOM and all document elements and
formatting are simply loaded into memory. Please read the following
articles for more information on DOM:

http://www.aspose.com/docs/display/wordsnet/Object+Model+Overview
http://www.aspose.com/docs/display/wordsnet/Composition+Diagrams

Regarding the shared interop code, there is no exact corresponding/matching classes/methods in Aspose.Words. However, you can achieve your requirements using Aspose.Words. Please read about DocumentBuilder, moving the cursor and inserting document elements from here:
http://www.aspose.com/docs/display/wordsnet/DocumentBuilder+Overview
http://www.aspose.com/docs/display/wordsnet/Moving+the+Cursor
http://www.aspose.com/docs/display/wordsnet/Inserting+Document+Elements

Moreover, The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages. Please read about Aspose.Words.Layout namespace from here:
http://www.aspose.com/docs/display/wordsnet/Aspose.Words.Layout+namespace

It would be great if you please share the complete scenario about your requirements. We will then provide you more information on this along with code.