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();
}
}