I am trying to convert a text file to pdf using the code lines below…
Hi Kulsum,
I tried with the latest and still the same issue.
Hi Kulsum,
Thanks for your inquiry.
Please note that Aspose.Words tries to mimic the way the Microsoft Word works. 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 produced on my side here for your reference. Secondly, to remove margins from the top/left sides in PDF, please try running 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 to the 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.