Trouble converting a DWG to PDF

I have a project to convert DWG drawings to PDFs as needed. The process will need to completed in a desktop environment as well as web environment. Using the Aspose.CAD .NET v26.1.0. I created C# .NET dll that will convert the documents. When the dll is called from a desktop application. It will convert the drawing just fine and saves the drawing as excepted. However, when the dll is called from the web environment to convert the exact same dwg file that was just converted by the desktop application. It throws this error: Drawing loading failed: Cannot process loading further due to incorrect file format structure. When attempts to load the image.

I have looked at another topic: https://forum.aspose.com/t/aspose-cad-25-10-1-dwg-pdf/320789
and it doesn’t seem to apply in the insistence.

  1. the Aspose.CAD.LoadOptions doesn’t appear to be a namespace any longer.
  2. the DWG image version doesn’t seem to be an issue or it wouldn’t work in the desktop version.
  3. The file isn’t Corrupted or incomplete file again because it can be converted using the desktop application.

Here is the method that is used to convert image:

private static System.IO.FileInfo ConvertAutoCad(System.IO.FileInfo input, string outputDir, string fileName)
{
    try
    {
        string lic = Path.Combine(Path.GetDirectoryName(General._StartLocation), General._LicenseName);
        License license = new License();
        license.SetLicense(lic);
        string newName = string.Empty;
        newName = fileName.Replace(input.Extension, ".pdf");

        System.IO.FileInfo output = new FileInfo(System.IO.Path.Combine(outputDir, newName));
        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(input.FullName)))
        {
            using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(ms))
            {
                Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
                rasterizationOptions.PageWidth = 1800;
                rasterizationOptions.PageHeight = 1150;
                rasterizationOptions.AutomaticLayoutsScaling = true;
                rasterizationOptions.NoScaling = false;
                rasterizationOptions.UnitType = UnitType.Centimenter;
                //rasterizationOptions.Layouts = new string[] { "Model" };
                //rasterizationOptions.Margins = new Margins()
                //{
                //    Top = 0,
                //    Bottom = 0,
                //    Left = 0,
                //    Right = 0
                //};
                // Create an instance of PdfOptions
                Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
                // Set the VectorRasterizationOptions property
                pdfOptions.VectorRasterizationOptions = rasterizationOptions;
                image.Save(output.FullName, pdfOptions);
            }
        }
        return output;
    }
    catch
    {
        throw;
    }
}

@thomas.greely

You reported an issue where a DWG file converts successfully in a desktop environment but fails in a web environment with the error: Drawing loading failed: Cannot process loading further due to incorrect file format structure . This suggests a potential inconsistency in how the file is being loaded or streamed between the two environments.

The error likely points to how the file stream is handled—specifically, using a MemoryStream might introduce differences in behavior between desktop and web contexts .

Recommended Fix:

Try using the file path directly instead of loading the file into a MemoryStream. This often resolves issues related to stream handling in different runtime environments.

Here’s a revised version of your method:

private static System.IO.FileInfo ConvertAutoCad(System.IO.FileInfo input, string outputDir, string fileName)
{
 try
 {
 string lic = Path.Combine(Path.GetDirectoryName(General._StartLocation), General._LicenseName);
 License license = new License();
 license.SetLicense(lic);

 string newName = fileName.Replace(input.Extension, ".pdf");
 System.IO.FileInfo output = new FileInfo(System.IO.Path.Combine(outputDir, newName));

 using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(input.FullName))
 {
 Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
 rasterizationOptions.PageWidth = 1800;
 rasterizationOptions.PageHeight = 1150;
 rasterizationOptions.AutomaticLayoutsScaling = true;
 rasterizationOptions.NoScaling = false;
 rasterizationOptions.UnitType = UnitType.Centimenter;

 Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
 pdfOptions.VectorRasterizationOptions = rasterizationOptions;

 image.Save(output.FullName, pdfOptions);
 }

 return output;
 }
 catch
 {
 throw;
 }
}

This version avoids MemoryStream and loads the file directly from disk, which typically prevents issues caused by differences in stream handling between desktop and web environments .

Let me know if this helps or if further investigation is needed.

That is how I initially had the method setup. Their was an issue with doing it that way. The dwg file is under source control by other software and has the attribute of read only. Aspose.CAD wouldn’t read file until I made a copy of the file and removed the read only attribute. Then it would throw the exact same error using the web method. Therefore, I changed it to use a memorystream to bypass the steps of coping the drawing to a holding area and removing the read only attribute.

@thomas.greely

You reported an issue where Aspose.CAD fails to read read-only DWG files, which is why you had to copy the file and remove the read-only attribute . You then switched to using a MemoryStream to bypass this problem .

Using a MemoryStream can help avoid file locking or attribute-related issues by loading the file content into memory directly . However, there’s a known inconsistency in how streams are handled between desktop and web environments, which can lead to errors even when using MemoryStream .

To resolve this, a recommended approach is to load the file directly from disk instead of using a MemoryStream, as this avoids potential stream handling discrepancies between environments .

Here’s a revised version of your method that loads the file directly from disk:

private static System.IO.FileInfo ConvertAutoCad(System.IO.FileInfo input, string outputDir, string fileName)
{
 try
 {
 string lic = Path.Combine(Path.GetDirectoryName(General._StartLocation), General._LicenseName);
 License license = new License();
 license.SetLicense(lic);

 string newName = fileName.Replace(input.Extension, ".pdf");
 System.IO.FileInfo output = new FileInfo(System.IO.Path.Combine(outputDir, newName));

 using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(input.FullName))
 {
 Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
 rasterizationOptions.PageWidth = 1800;
 rasterizationOptions.PageHeight = 1150;
 rasterizationOptions.AutomaticLayoutsScaling = true;
 rasterizationOptions.NoScaling = false;
 rasterizationOptions.UnitType = UnitType.Centimenter;

 Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
 pdfOptions.VectorRasterizationOptions = rasterizationOptions;

 image.Save(output.FullName, pdfOptions);
 }

 return output;
 }
 catch
 {
 throw;
 }
}

This version avoids MemoryStream and loads the file directly from disk, which typically prevents issues caused by differences in stream handling between desktop and web environments . Let me know if this helps or if further investigation is needed.

Doing the way you suggested it throws this error: Access to the path ‘\Get-srv04\Vault\5\000221E5\00000003.DWG’ is denied.
\GET-srv04\Vault is shared to everyone with read access.

When I switched it back doing it the way I initially did it get this error again: Drawing loading failed: Cannot process loading further due to incorrect file format structure.

Here is the methods I’m using:

private static System.IO.FileInfo ConvertAutoCad(System.IO.FileInfo input, string outputDir, string fileName)
{
    try
    {
        string lic = Path.Combine(Path.GetDirectoryName(General._StartLocation), General._LicenseName);
        License license = new License();
        license.SetLicense(lic);
        string newName = string.Empty;
        newName = fileName.Replace(input.Extension, ".pdf");
        FileInfo tempDrawing = MakeTempFile(input);
        try
        {

            System.IO.FileInfo output = new FileInfo(System.IO.Path.Combine(outputDir, newName));
            using(MemoryStream ms = new MemoryStream(File.ReadAllBytes(input.FullName)))
            {
                using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(tempDrawing.FullName))
                {
                    Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
                    rasterizationOptions.PageWidth = 1800;
                    rasterizationOptions.PageHeight = 1150;
                    rasterizationOptions.AutomaticLayoutsScaling = true;
                    rasterizationOptions.NoScaling = false;
                    rasterizationOptions.UnitType = UnitType.Centimenter;
                    //rasterizationOptions.Layouts = new string[] { "Model" };
                    //rasterizationOptions.Margins = new Margins()
                    //{
                    //    Top = 0,
                    //    Bottom = 0,
                    //    Left = 0,
                    //    Right = 0
                    //};
                    // Create an instance of PdfOptions
                    Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
                    // Set the VectorRasterizationOptions property
                    pdfOptions.VectorRasterizationOptions = rasterizationOptions;
                    image.Save(output.FullName, pdfOptions);
                }
            }
            if (tempDrawing.Exists)
            {
                tempDrawing.Delete();
            }
            return output;
        }
        catch
        {
            throw;
        }
        finally
        {
            if(tempDrawing != null && tempDrawing.Exists)
            {
                tempDrawing.Delete();
            }
        }
    }
    catch
    {
        throw;
    }
}
private static FileInfo MakeTempFile(FileInfo source)
{
    try
    {
        string tempFolder = Path.Combine(Path.GetDirectoryName(General._StartLocation), General._TempFolder);
        if (Directory.Exists(tempFolder) == false)
        {
            Directory.CreateDirectory(tempFolder);
        }
        FileInfo tempDrawing = new FileInfo(Path.Combine(tempFolder, source.Name));
        File.Copy(source.FullName, tempDrawing.FullName, true);
        var attr = File.GetAttributes(tempDrawing.FullName);
        if((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            attr = attr & ~FileAttributes.ReadOnly;
            File.SetAttributes(tempDrawing.FullName, attr);
        }
        return tempDrawing;
    }
    catch
    {
        throw;
    }
}

I have attached the copied drawing to show it is not corrupt or incomplete.
00000003.zip (79.0 KB)

@thomas.greely,
Hi,
could you please try if this loading is helpful (you can try it directly for file with read only attribute):

using (var fileStream = new FileStream("00000003.dwg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
	using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileStream))
	{
        ...
	}
}

Thank you for the suggest.
I did try it and got the same error. I also tried using the FileStream in place of MemoryStream and also got the same error.

Aspose.CAD.CadExceptions.ImageLoadException
  HResult=0x80131500
  Message=Drawing loading failed: Cannot process loading further due to incorrect file format structure.
  Source=Aspose.CAD
  StackTrace:
   at Aspose.CAD.Image.#=z0$dRbn$ieg6c(StreamContainer #=zr8E88lHN38cN, LoadOptions #=z9C$pAv$jcEfd)
   at Aspose.CAD.Image.Load(Stream stream, String fileName, LoadOptions loadOptions)
   at Aspose.CAD.Image.Load(Stream stream, LoadOptions loadOptions)
   at Aspose.CAD.Image.Load(Stream stream)
   at DocumentLookup.CompanionDocumentHandler.ConvertAutoCad(FileInfo input, String outputDir, String fileName) in C:\GitBucket\Utilities\DocumentLookup_App\DocumentLookup\CompanionDocumentHandler.cs:line 505

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
Exception: Cannot process loading further due to incorrect file format structure.

Inner Exception 2:
NotSupportedException: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

Updated method with your suggestion.

private static System.IO.FileInfo ConvertAutoCad(System.IO.FileInfo input, string outputDir, string fileName)
{
    try
    {
        string lic = Path.Combine(Path.GetDirectoryName(General._StartLocation), General._LicenseName);
        License license = new License();
        license.SetLicense(lic);
        string newName = string.Empty;
        newName = fileName.Replace(input.Extension, ".pdf");
        FileInfo tempDrawing = MakeTempFile(input);
        try
        {

            System.IO.FileInfo output = new FileInfo(System.IO.Path.Combine(outputDir, newName));
            using (FileStream fs = new FileStream(input.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            //using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(input.FullName)))
            {
                using (CadImage image = (CadImage)Aspose.CAD.Image.Load(fs))
                //using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(tempDrawing.FullName))
                {
                    Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
                    rasterizationOptions.PageWidth = 1800;
                    rasterizationOptions.PageHeight = 1150;
                    rasterizationOptions.AutomaticLayoutsScaling = true;
                    rasterizationOptions.NoScaling = false;
                    rasterizationOptions.UnitType = UnitType.Centimenter;
                    //rasterizationOptions.Layouts = new string[] { "Model" };
                    //rasterizationOptions.Margins = new Margins()
                    //{
                    //    Top = 0,
                    //    Bottom = 0,
                    //    Left = 0,
                    //    Right = 0
                    //};
                    // Create an instance of PdfOptions
                    Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
                    // Set the VectorRasterizationOptions property
                    pdfOptions.VectorRasterizationOptions = rasterizationOptions;
                    image.Save(output.FullName, pdfOptions);
                }
            }
            if (tempDrawing.Exists)
            {
                tempDrawing.Delete();
            }
            return output;
        }
        catch
        {
            throw;
        }
        finally
        {
            if(tempDrawing != null && tempDrawing.Exists)
            {
                tempDrawing.Delete();
            }
        }
    }
    catch
    {
        throw;
    }
}

@thomas.greely,

We have created investigation ticket CADNET-10351 to reproduce the issue, but as same code works for the desktop app, and not for web app, the problem could be not in the dll. It may be helpful for us if you can create some small demo web app similar to your with minimal code to reproduce, so we can run it and see the error.

I uploaded a small simple web app using the same DLL that handles the conversion to a GitHub repo because the DLL zipped even zipped it pretty big.

@thomas.greely,
thank you, we have received the project. I have removed the URL in your post above, and could you please remove the project from public access as soon as possible. Let us investigate it and come back with results.

Thank you for the edit and looking into this. I look forward to hearing back from you.

1 Like

@thomas.greely,
Could you please try to register the encoding provider (for instance in the Program.cs file):

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

That is what I was missing. I knew it had to something silly I forgot. Thank you for all your help with this.

@thomas.greely,
we are happy to help :slight_smile: