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

Free Support Forum - aspose.com

Converting text to pdf

I am trying to convert a text file to pdf using the code lines below…


Aspose.Words.Document doc = new Aspose.Words.Document(textFilePath);
doc.Save(pdfFileName, Aspose.Words.SaveFormat.Pdf);

I am attaching the text file: Test1.txt
Converted Pdf file from Aspose: Test1.pdf

I have the following issues with the pdf file:
* The tons of extra margin on top and left
* The font seems to be bigger than the text file.
* The single line breaks are missing

Is there any way, I can fix these issues.

Thanks,
Kulsum

Hi Kulsum,


Thanks for your inquiry. First of all, I would suggest you please upgrade to the latest version of Aspose.Words i.e. 13.5.0 from the following link:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

I hope, this helps.

Also, please note that Aspose.Words page layout engine tries to mimic the way the Microsoft Word’s page layout engine works. To you, this means that if you convert your TXT file into PDF, XPS or print it using Aspose.Words, the output will appear almost exactly as if it was done by Microsoft Word.

Best regards,

I tried with the latest and still the same issue.


Is there any other component from Aspose that is recommended for txt to pdf conversion.

Thanks.

Hi Kulsum,


Thanks for your inquiry.

Please note that Aspose.Words tries to mimic the way the Microsoft Word works. To you, this means that if you convert a text file into PDF using Aspose.Words, the output will appear almost exactly as if it was done by Microsoft Word. I have attached the output PDF document i.e. produced on my side here for your reference. Secondly, to be able to remove margins from top/left sides in PDF, please try run the following code snippet:

Aspose.Words.LoadOptions
loadOptions =
new Aspose.Words.LoadOptions();

loadOptions.LoadFormat = LoadFormat.Text;

Document doc = new Document(@"C:\Temp\test1.txt", loadOptions);

doc.FirstSection.PageSetup.TopMargin = 0;

doc.FirstSection.PageSetup.LeftMargin = 0;

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


I hope, this helps.
Kulsum:
Is there any other component from Aspose that is recommended for txt to pdf conversion.
As your query involves multiple Aspose components, I am moving your thread in Aspose.Total forum where you will be guided appropriately.

Best regards,
kulsum.arif:
Is there any other component from Aspose that is recommended for txt to pdf conversion.

Hi Kulsum,

Adding more to Awais’s comments, we also have a component named Aspose.Pdf for .NET which supports the feature to create as well as manipulate existing PDF files. It also offers the feature to convert text files to PDF format. Please note that during the conversion process, you can specify the formatting of text and also specify the Top, Left, Right, Bottom margin information. Please try using the following code snippet to accomplish your requirements.

For your reference, I have also attached the resultant PDF file which is generated over my end.

[C#]

// read source text file
System.IO.TextReader tr = new StreamReader("c:/pdftest/Test1.txt");
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a new section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
// set left and top margin for page
sec1.PageInfo.Margin.Left = sec1.PageInfo.Margin.Top = 5;
//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Generator.Text t2 = new Aspose.Pdf.Generator.Text(tr.ReadToEnd());
// set the font size for text inside PDF file
t2.TextInfo.FontSize = 12;
// add text to paragraphs collection of section object
sec1.Paragraphs.Add(t2);
// save the PDF file
pdf1.Save(“c:/pdftest/Test1.txt.Pdf”);

For further information, please visit How to Convert a text file to PDF.