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

Free Support Forum - aspose.com

Converting a plain text file to PDF goes incredibly slowly and produces poor results

Hi all! It’s basically as the title says. I have a situation where I am combining documents which may include text files which can be multiple pages long.

As has been mentioned in various articles, using TextBuilder.AppendText doesn’t work; it just puts everything on a single line at the bottom of the page. It does have a parameter for a list of TextFragment objects though.
TextBuilder.AppendParagraph is the suggested method but that works very, very slowly and I don’t see any way to automatically add page breaks. If I wait the 30 minutes+ for it to finish then it still only has a single page displayed and looks terrible.

So, how can I simply import text file contents with page breaks? Nothing fancy in terms of formatting, just a basic display like I’d see in a text editor. The output is a pdf overall so I can’t just return a text file.

Anyway, here is what I tried last. I tried different variations of this but never produce acceptable output no matter how long I wait. I tried adding the textLines block in one operation, etc.

Any suggestions? Thanks!
Edit: Is there any way to add a page size and automatically add new pages?

byte[] byteResults = null;

using ( var document = new Aspose.Pdf.Document() )
{
	document.PageInfo.Height = PageSize.PageLetter.Height;
	document.PageInfo.Width = PageSize.PageLetter.Width;
	var newPage = document.Pages.Add();
	TextBuilder builder = new TextBuilder( newPage );

	string[] textLines = Encoding.UTF8.GetString( documentBytes ).Split(
			 new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None );

	var paragraph = new TextParagraph();
	paragraph.Justify = false;
	paragraph.FormattingOptions = new TextFormattingOptions( TextFormattingOptions.WordWrapMode.ByWords );
	paragraph.HorizontalAlignment = HorizontalAlignment.Left;
	paragraph.VerticalAlignment = VerticalAlignment.Top;

	foreach ( var line in textLines )
	{
		TextFragment fragment1 = new TextFragment( line );
		fragment1.Position = new Position( 0, 0 );
		fragment1.TextState.Font = FontRepository.FindFont( "Courier" );
		fragment1.TextState.FontSize = 10;
		fragment1.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb( System.Drawing.Color.White );
		fragment1.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb( System.Drawing.Color.Black );
		paragraph.AppendLine( fragment1 );
	}

	builder.AppendParagraph( paragraph );

	using ( var pdfStream = new MemoryStream() )
	{
		document.Save( pdfStream, SaveFormat.Pdf );
		byteResults = pdfStream.ToArray();
	}
}

While I’d like to know if there is a better way to do it, this worked well enough. It seems the long processing occurred by having the content all accumulate on the same page. I manually forced new pages by counting lines which isn’t ideal but worked. I wanted the resulting document converted to bytes but obviously other developers might want to do something else with the document.

byte[] byteResults = null;
var pageSize = 60;
var pageWidth = 132;

var textLines = new List<string>( Encoding.UTF8.GetString( documentBytes ).Split(
	new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None ) );
var newLines = new List<string>();
for ( var i = 0; i < textLines.Count; i++ )
{
	if ( textLines[i].Length > pageWidth )
	{
		var lineSplit = Split( textLines[i], pageWidth );
		textLines[i] = lineSplit[0];
		foreach ( var line in lineSplit )
		{
			newLines.Add( line );
		}
	}
	else
	{
		newLines.Add( textLines[i] );
	}
}
textLines = null;

using ( var document = new Aspose.Pdf.Document() )
{
	document.PageInfo.Height = PageSize.PageLetter.Height;
	document.PageInfo.Width = PageSize.PageLetter.Width;

	var lineCount = 999;
	Page newPage = null;
	TextBuilder builder = null;
	TextParagraph paragraph = null;
	foreach ( var line in newLines )
	{
		if ( lineCount > pageSize )
		{
			newPage = document.Pages.Add();
			builder = new TextBuilder( newPage );
			paragraph = new TextParagraph();
			paragraph.Justify = false;
			paragraph.FormattingOptions = new TextFormattingOptions( TextFormattingOptions.WordWrapMode.NoWrap );
			paragraph.HorizontalAlignment = HorizontalAlignment.Left;
			paragraph.VerticalAlignment = VerticalAlignment.Top;
			paragraph.Margin.Left = 25;
			paragraph.Rectangle = new Aspose.Pdf.Rectangle( 0, 0, 550, 800 );
			lineCount = 0;
		}
		TextFragment fragment1 = new TextFragment( line );
		fragment1.TextState.Font = FontRepository.FindFont( "CourierNew" );
		fragment1.TextState.FontSize = 6;
		paragraph.AppendLine( fragment1 );
			
		lineCount++;

		if ( lineCount >= pageSize )
		{
			builder.AppendParagraph( paragraph );
		}
	}

	using ( var pdfStream = new MemoryStream() )
	{
		document.Save( pdfStream, SaveFormat.Pdf );
		byteResults = pdfStream.ToArray();
	}
}

@williamfa

Please try using the below code snippet and see if it helps in getting good performance. In case you still notice delay, please share a sample .txt file for our reference so that we can test the scenario in our environment and address it accordingly.

System.IO.TextReader tr = new StreamReader(dataDir + "input.txt", Encoding.UTF8, true);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
String strLine;
TextBuilder builder = new TextBuilder(page);
double x = 100; double y = 100;
while ((strLine = tr.ReadLine()) != null)
{
 Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment(strLine);
 //page.Paragraphs.Add(text);
 text.Position = new Position(x, y);
 if (y >= page.PageInfo.Height - 72)
 {
  y = 100;
  page.Dispose();
  page = doc.Pages.Add();
  builder = new TextBuilder(page);
 }
 else
 {
  y += 15;
 }
 builder.AppendText(text);
}
doc.Save(dataDir + "TexttoPDF_out.pdf");
tr.Close();