Free Support Forum - groupdocs.com

Error in converting plain text file to Pdf


#1

Hi,
I tried to convert a simple .txt file to Pdf:


var config = new ConversionConfig()
{
OutputPath = Combine(workingFolder, “temp”),
CachePath = Combine(workingFolder, “temp”),
LocalesPath = Combine(workingFolder, “temp”),
StoragePath = Combine(workingFolder, “temp”),
UseCache = false
};
GroupDocs.Conversion.Handler.ConversionHandler handler = new GroupDocs.Conversion.Handler.ConversionHandler(config);
PdfSaveOptions saveOptions = new PdfSaveOptions()
{
ConvertFileType = PdfSaveOptions.PdfFileType.Pdf,
PageNumber = 1,
NumPagesToConvert = 100,
};

var memStream = new MemoryStream(File.ReadAllBytes(@“e:\simple.txt”);
memStream.Seek(0, SeekOrigin.Begin);
try
{
var stream = handler.Convert(new MemoryStream(content), saveOptions);
stream.Seek(0, SeekOrigin.Begin);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
base.StoreDocumentContent(docId, docName,extension, bytes);
}
catch (Exception ex)
{
throw ex;
}

But I get the following exception upon first line in try block:
"Attempted to read past the end of the stream."

StackTrace:
at XXX.Workshop.Form1.button3_Click(Object sender, EventArgs e) in E:\Code\XXX\Form1.cs:line 136
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at XXX.Program.Main() in E:\Code\XXX\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

#2

Hello,


Thank you for giving a try to the new GroupDocs.Conversion API

We have tested this code and found that the older version of GroupDocs.Conversion for .NET API through this exception, but this was resolved in new version 3.1.0

Please download new version from NuGet here it will resolve the issue.

If you will need any help or you will have any other questions please feel free to ask.

Warm Regards


#3

Hi Muhammad,
I’m using version 3.1.0. I made a simple project to demonstrate it. Please download it from the following link:

https://drive.google.com/file/d/0B6C2p6WGgdycUS1rVjlsM3VwRzQ/view?usp=sharing


#4

Hi Leap,


Thank you for providing the sample project.

We’ll investigate further and let you know our findings soon.

Best Regards

#5

Hi leap ,

We just changed the Project Properties and Target Framework to .NET Framework 4.5 of your provided sample project and found no issue in it, everything works great.

If you will need any help or you will have any other questions please feel free to ask.


Warm Regards,



#6

Muhammad,
I tried the same and no success. Here is the project with .net framework ver 4.5:

https://drive.google.com/open?id=0B6C2p6WGgdycYU5DbHVfQkpxNEk


Am I missing something?

#7

Hello,


We have run this project with .NET Framework 4.5 and GroupDocs.Conversion 3.1.0, it is working properly.

  • It Reads text file into Stream
  • Convertes the Stream Input to PDF file
  • Returns Output PDF file as Stream

If you want to see the output file on physical drive then you can use this code

using GroupDocs.Conversion.Config;
using GroupDocs.Conversion.Converter.Option;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DocConvert
{
class Program
{
static void Main(string[] args)
{
string workingFolder = @“e:”;
string textFilePath = @“e:\test.txt”;

var config = new ConversionConfig()
{
OutputPath = Combine(workingFolder, “temp”),
CachePath = Combine(workingFolder, “temp”),
LocalesPath = Combine(workingFolder, “temp”),
StoragePath = Combine(workingFolder, “temp”),
UseCache = false
};
GroupDocs.Conversion.Handler.ConversionHandler handler = new GroupDocs.Conversion.Handler.ConversionHandler(config);
PdfSaveOptions saveOptions = new PdfSaveOptions()
{
ConvertFileType = PdfSaveOptions.PdfFileType.Pdf,
PageNumber = 1,
NumPagesToConvert = 1,
OutputType=OutputType.String
};

try
{
var memStream = new MemoryStream(File.ReadAllBytes(textFilePath));
var stream = handler.Convert(memStream, saveOptions);
//stream.Seek(0, SeekOrigin.Begin);
//var bytes = new byte[stream.Length];
//stream.Read(bytes, 0, (int)stream.Length);
}
catch (Exception ex)
{
throw ex;
}
}

private static string Combine(string workingFolder, string folder)
{
return Path.Combine(workingFolder, folder);
}
}
}

  • This will Read text file into Stream
  • Convert Stream Input to PDF
  • Returns Converted file physical Path as "E:/temp/2351bad7de63467c9529f4ffdf9f0c4f.pdf"


Please share your findings and requirements in detail so we can suggest relevant solution.

Warm Regards

#8

Hi Muhammad.
Thanks for consideration so far.

I copied and pasted the code: No Success and the same exact exception.
I’m very curious to know if you tested on the very last solution I provided. It’s .in net 4.5 and is using groupdocs.conversion 3.1.0.

By the way, I install groupdocs.conversion using Nuget:

Install-Package groupdocs-conversion-dotnet


I hope this information helps.


Bests


#9

Hi,


I am sorry to hear that you are still facing such issues, we have already tested your last posted code which was working fine.

Now we are attaching the working new project with same code. kindly download and build this project and see the output.


Please share your findings in detail (Exceptions/Snapshots) so we can help you to resolve this issue.

Attached “Sample - DocConvert By GroupDocs-Support.rar” contains VS 2012 OR higher solution also with Sample “Test.txt” file.

Warm Regards

#10

Hi Muhammd,
Unfortunately, it didn’t work. but:

1- The only overload by which I could generate a proper pdf file (and not a stream) was:

handler.Convert(fileStream, outputFileName, stringSaveOptions);

2- I tried to use this overload to receive back a stream:

handler.Convert(fileStream, outputFileName, streamSaveOptions);
and it worked as well.


#11

Hi,


Thank you for the details, yes there are many ways to get required conversion.

Please find below some resources to quickly get started with GroupDocs.Conversion API.


<o:p></o:p>

If you will need any help or you will have any other questions please feel free to ask.


Warm Regards