Hi,
I use this code for convert Text to PDF.
Dim objDoc As Document = New Document(“README_proc.txt”)
objDoc.Save(“README_proc.pdf”)
I would like to know, if it’s possible to change the font size to use for the convertion before to load file text.
Thank you
@jechevarria,
Thanks for your inquiry. Please use the following code example to change the font name and size of text document and convert it to PDF. Hope this helps you.
Dim doc As New Document(MyDir + "in.txt")
Dim changer As New FontChanger()
doc.Accept(changer)
doc.Save(MyDir + "output.pdf")
''' <summary>
''' Class inherited from DocumentVisitor, that chnges font.
''' </summary>
Class FontChanger Inherits DocumentVisitor
''' <summary>
''' Called when a FieldEnd node is encountered in the document.
''' </summary>
Public Overrides Function VisitFieldEnd(fieldEnd As FieldEnd) As VisitorAction
'Simply change font name
ResetFont(fieldEnd.Font)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a FieldSeparator node is encountered in the document.
''' </summary>
Public Overrides Function VisitFieldSeparator(fieldSeparator As FieldSeparator) As VisitorAction
ResetFont(fieldSeparator.Font)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a FieldStart node is encountered in the document.
''' </summary>
Public Overrides Function VisitFieldStart(fieldStart As FieldStart) As VisitorAction
ResetFont(fieldStart.Font)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a Footnote end is encountered in the document.
''' </summary>
Public Overrides Function VisitFootnoteEnd(footnote As Footnote) As VisitorAction
ResetFont(footnote.Font)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a FormField node is encountered in the document.
''' </summary>
Public Overrides Function VisitFormField(formField As FormField) As VisitorAction
ResetFont(formField.Font)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a Paragraph end is encountered in the document.
''' </summary>
Public Overrides Function VisitParagraphEnd(paragraph As Paragraph) As VisitorAction
ResetFont(paragraph.ParagraphBreakFont)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a Run node is encountered in the document.
''' </summary>
Public Overrides Function VisitRun(run As Run) As VisitorAction
ResetFont(run.Font)
Return VisitorAction.[Continue]
End Function
''' <summary>
''' Called when a SpecialChar is encountered in the document.
''' </summary>
Public Overrides Function VisitSpecialChar(specialChar As SpecialChar) As VisitorAction
ResetFont(specialChar.Font)
Return VisitorAction.[Continue]
End Function
Private Sub ResetFont(font As Aspose.Words.Font)
font.Name = mNewFont
font.Size = mSize
End Sub
'Font by default
Private mNewFont As String = "Arial"
Private mSize As Double = 14.0
End Class
@jechevarria
Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.
Hi,
I use LoadOptions for disable AllowTrailingWhiteSpaceForListItems
Dim text = New LoadOptions()
text.AllowTrailingWhitespaceForListItems = False
text.WarningCallback = New WarningCall()
text.LoadFormat = LoadFormat.Text
Dim objDoc As Document = New Document("ProblemeConvert.txt", text)
Dim changer As New FontChanger()
objDoc.Accept(changer)
The problem is that ListItem not change the font,see file ProblemeConvert.pdf
ProblemeConvert.pdf (19.4 KB)
@jechevarria,
Thanks for your inquiry. Please add the following method in FontChanger class to change the font of list number.
Public Overrides Function VisitParagraphStart(paragraph As Paragraph) As VisitorAction
If paragraph.IsListItem Then
ResetFont(paragraph.ListFormat.ListLevel.Font)
End If
Return VisitorAction.[Continue]
End Function
Thank you! It works!
Is it possible to remove space between exemple “1. Access” ?
Thanks
Hi,
I used this line for remove space
paragraph.ListFormat.ListLevel.TrailingCharacter = Lists.ListTrailingCharacter.Space
Do you have a better solution?
Thank you
@jechevarria,
Thanks for your inquiry. You are using the correct property for your requirement. ListLevel.TrailingCharacter property gets or sets the character inserted after the number for the list level. Please use following line of code in VisitParagraphStart method.
paragraph.ListFormat.ListLevel.TrailingCharacter = ListTrailingCharacter.Space
Hi,
Another problem when converting Text to PDF is why the TAB does not take the same space in the whole case (see line 4.)
Is it possible to redefine the space that takes the character TAB?
Capture.PNG (3.5 KB)
@jechevarria,
Thanks for your inquiry. Please ZIP and attach your input document here for testing. We will investigate the issue on our side and provide you more information.
Hi,
In file zip ProblemeConvert.zip (11.8 KB)
My code test (Module.vb), input (ProblemeConvert.txt) et output pdf (ProblemeConvert.pdf), As you can see in file PDF, the space for the character TAB not is the same.
thanks you
@jechevarria,
Thanks for your inquiry. The text line starts with “A, B, ?, C.” is not list item. LoadOptions.AllowTrailingWhitespaceForListItems property allows to specify how numbered list items are recognized when document is imported from plain text format.
If set to true, lists recognition algorithm allows list numbers to end with either dot or whitespace character.
If this option is set to false then the list item is only recognized as such if the leading number is ending with dot “.” symbol.
Could you please share some more detail about your use case? We will then provide you more information on this along with code.
hi,
This zip file is correct ProblemeConvertAspose.zip (11.6 KB).
The file ProblemeConvertAspose.txt is the source and the file ProblemeConvert.pdf is output. I would like that the content of file PDF output is the same to the file text.
How Can I to do this?
@echevarria,
Thanks for sharing the detail. We have modified the VisitParagraphStart method according to your requirement. We have attached the output PDF for your kind reference. Hope this helps you.
output.pdf (14.7 KB)
Public Overrides Function VisitParagraphStart(paragraph As Paragraph) As VisitorAction
If paragraph.IsListItem Then
ResetFont(paragraph.ListFormat.ListLevel.Font)
paragraph.ListFormat.ListLevel.TrailingCharacter = ListTrailingCharacter.Space
paragraph.Range.Replace(ControlChar.Tab, "", New FindReplaceOptions())
Else
Dim text As [String] = paragraph.ToString(SaveFormat.Text)
If text.StartsWith("A") OrElse text.StartsWith("B") OrElse text.StartsWith("C") OrElse text.StartsWith("?") OrElse text.StartsWith("3.") Then
If text.Length > 1 AndAlso text.IndexOf(".") = 1 Then
paragraph.Range.Replace(ControlChar.Tab, " ", New FindReplaceOptions())
Else
paragraph.Range.Replace(ControlChar.Tab, " ", New FindReplaceOptions())
End If
End If
End If
Return VisitorAction.[Continue]
End Function
hi,
Is it possible to disable any detection in the received text and transform it into PDF as is?
Thanks you
@echevarria,
Thanks for your inquiry. Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further. Thanks for your cooperation.
Hi, I would like to disable the lists recognition algorithm completely. is it possible? If it isn’t possible, can I to define other characters as List, Example: “?”
@jechevarria,
Thanks for your inquiry. You can remove the list items from the document using following code example. Hope this helps you.
Dim opt As New Aspose.Words.LoadOptions()
opt.AllowTrailingWhitespaceForListItems = False
Dim doc As New Aspose.Words.Document(MyDir + "ProblemeConvertAspose.txt", opt)
For Each para As Paragraph In doc.GetChildNodes(NodeType.Paragraph, True)
If para.IsListItem Then
para.ListFormat.RemoveNumbers()
End If
Next