Did not convert from memory stream to pdf If memory stream have some graph/chart

I am using the below code to convert pdf from a word document/memory stream. but when I try to open the pdf which is generated by code the PDF did not open and shows the message “We can not open this file…something went wrong.” Please help me to sort out that issue.

MemoryStream objMemoryStream = new MemoryStream(document.FileData);
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense(licensePath);
Aspose.Words.Document doc = new Aspose.Words.Document(objMemoryStream);
Aspose.Words.Saving.PdfSaveOptions saveOption = new Aspose.Words.Saving.PdfSaveOptions
{
    Compliance = Aspose.Words.Saving.PdfCompliance.PdfA1b,
    SaveFormat = Aspose.Words.SaveFormat.Pdf,
    MemoryOptimization = true
};
doc.Save(tempFilePath, saveOption);
objMemoryStream.Dispose();

@anand.nagar Could you please attach your problematic document and output here for testing? We will check the issue and provide you more information.

Main Office.docx (50.5 KB)

@Konstantin.Kornilov I have uploaded document which gives me error and not converting to pdf properly.
Thanks for reply.

@anand.nagar Thank you for additional information. I have checked the conversion on my side and the output PDF works fine on my side. I can open and view it using Acrobat Reader. Could you please attach your output PDF document here for our reference?
I have used the latest 22.11 version of Aspose.Words for testing? here is the output produced on my side: out.pdf (53.1 KB)

I am using 18.0 verson

@anand.nagar Thank you for additional information. 18.x version has been released more than 4 years ago. Please try using the latest version of Aspose.Words and let us know if the problem still persist on your side. And if so, please create a simple application that will allow us to reproduce it.

I have downloaded Aspose library 22.11.00 version but issue is still exists.

@anand.nagar Could you please zip and attach your output PDF document here for our reference? We will check it and provide you more information.

Hi Alexey,
I am trying to upload word file and convert it to memory stream its working fine, but when I convert memory stream to pdf then its returning error message given below -
I have attached file which is showing error on pdf conversion.

Main OfficeNew.docx (50.6 KB)

@anand.nagar Unfortunately, I still cannot reproduce the problem on my side. Also, I do not see any error message in your replay. It looks like when you reply to e-mail and the message is forwarded to the forum part of information is lost. Could you please post directly in the forum to avoid this? Also, please attach the problematic output PDF produced on your side.

Hi Alexey,

The error message was “Not a PDF or corrupted”.

@anand.nagar Could you please attach the problematic output PDF produced on your side for our reference? We will check it and provide you more information. Also please provide code that you use to produce the problematic PDF document.

Hi Alexey,
I have docx file and I want to display as PDF. so I shared problematic docx file and unable to generate PDF of it.

@anand.nagar Unfortunately, the problem is not reproducible on my side using the latest version of Aspose.Words. I can successfully convert you document to PDF (see PDF document I have attached earlier) on my side.

Hi Alexey,

I am using below code

  1. converting docx file to memory stream
internal static string ProcessSingleUpload(HttpPostedFileBase file, DocumentModel doc)
{
    string result = string.Empty;
    int documentId = -1;

    byte[] fileData;
    int fileSize = file.ContentLength;

    try
    {
        using (BinaryReader reader = new BinaryReader(file.InputStream))
        {
            fileData = reader.ReadBytes((int)file.InputStream.Length);
        }
        DocumentFileModel newDocFile = new DocumentFileModel
        {
            Document = doc,
            FileName = Path.GetFileName(file.FileName),
            FileSize = fileSize,
            FileData = fileData,
            Extension = Path.GetExtension(file.FileName),
            MimeType = file.ContentType,
            UploadDate == DateTime.Now

        }
    catch (Exception ex)
    {
    }
}
  1. Converting MemoryStream to pdf file and display-
private static MemoryStream ConvertToPDFStream(DocumentFileModel document)
{
    string licensePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Properties/Aspose.Total.lnc");
    MemoryStream objMemoryStream = new MemoryStream(document.FileData);
    document.FileData = new byte[5];
    MemoryStream pdfStream;

    string tempFileName = Guid.NewGuid().ToString(),
    tempFilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DocumentConversion/") + tempFileName + ".pdf";

    Aspose.Words.License license = new Aspose.Words.License();
    license.SetLicense(licensePath);

    Aspose.Words.Document doc = new Aspose.Words.Document(objMemoryStream);

    Aspose.Words.Saving.PdfSaveOptions saveOption = new Aspose.Words.Saving.PdfSaveOptions
    {
        Compliance = Aspose.Words.Saving.PdfCompliance.PdfA1b,
        SaveFormat = Aspose.Words.SaveFormat.Pdf,
        MemoryOptimization = true
    };

    doc.Save(tempFilePath, saveOption);

    objMemoryStream.Dispose();
    break;

    using (FileStream fileStream = File.OpenRead(tempFilePath))
    {
        //create new MemoryStream object
        pdfStream = new MemoryStream();
        pdfStream.SetLength(fileStream.Length);
        //read file to MemoryStream
        fileStream.Read(pdfStream.GetBuffer(), 0, (int)fileStream.Length);
    }
    return pdfStream;
}
  1. Error Generated on that line ( license.SetLicense(licensePath) )

@anand.nagar The mentioned exception is thrown because you are using Aspose.Words version, which was released after your license subscription expiration date.
Every license for Aspose.Words provides a 1-year subscription for free upgrades to any new Aspose.Words version that comes out.
You can check when your subscription expires by opening the license file in Notepad (but take care not to modify and save the license file or it will no longer work) and see the SubscriptionExpiry field.

<SubscriptionExpiry>20220218</SubscriptionExpiry>

It means that you can free upgrade to version of Aspose.Words published before 02/18/2022.

If you need to update to the newer version, you should renew your license.

Hey Alexey,
Is it possible to use trial license for testing these things in my local machine? If it will work then I will share to my manager for renew the license.

Thanks,
Anand

@anand.nagar Sure, you can request a free 30-days temporary license to test the latest version without evaluation version limitations.

Hello Alexey,

Can you please help me to get Temporary license? I tried to get but couldn’t find a place where I request for that Temporary license.