A11.dwg.zip (8.6 MB)
错误日志如下:
Aspose.CAD.CadExceptions.ImageLoadException
HResult=0x80131500
Message=Drawing loading failed: Cannot process loading further due to incorrect file format structure. Error details: 给定关键字不在字典中。
Source=Aspose.CAD
StackTrace:
在 Aspose.CAD.Image.#=z4irEtv6UsN7Y(StreamContainer #=zAFU8HM$ZH4Y8, LoadOptions #=zzwR9DjTDR_Nc)
在 Aspose.CAD.Image.Load(String filePath)
在 WindowsFormsApp4.Form1.dwg2pdf(String infile) 在 C:\Users\Administrator\source\repos\WindowsFormsApp4\WindowsFormsApp4\Form1.cs 中: 第 38 行
在 WindowsFormsApp4.Form1.button1_Click(Object sender, EventArgs e) 在 C:\Users\Administrator\source\repos\WindowsFormsApp4\WindowsFormsApp4\Form1.cs 中: 第 29 行
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsFormsApp4.Program.Main() 在 C:\Users\Administrator\source\repos\WindowsFormsApp4\WindowsFormsApp4\Program.cs 中: 第 19 行
内部异常 1:
Exception: Cannot process loading further due to incorrect file format structure. Error details: 给定关键字不在字典中。
@PangXiaogang
Why the exception is thrown
ImageLoadException : “给定关键字不在字典中” is raised by Aspose.CAD when the library cannot recognise a keyword in the DWG file header. This usually happens for one (or more) of the following reasons:
| Reason |
What it means |
How to resolve it |
| Corrupted or incomplete file |
The binary data does not match any known DWG structure. |
Open the file in AutoCAD (or another DWG viewer). If it opens, re‑save it; if it does not, obtain a clean copy. |
| DWG version newer than the library supports |
Aspose.CAD 25.10.1 supports DWG up to the 2022 release. Newer versions (2023, 2024…) contain keywords the library does not know. |
Re‑save the drawing in an older AutoCAD version (DWG 2022 or earlier) or upgrade to a newer Aspose.CAD build that adds support for the newer version. |
| Wrong file inside the archive |
macOS archives often contain a __MACOSX/._YourFile.dwg resource‑fork entry. Loading that file fails. |
Extract the archive and use the real YourFile.dwg (ignore the __MACOSX folder). |
| Missing/incorrect load options (rare) |
Some DWG files need the loader to be forced to a specific version. |
Use CadLoadOptions and, if required, set LoadVersion. |
Step‑by‑step solution
1. Extract the DWG correctly
# PowerShell example
Expand-Archive -Path "C:\Temp\A11.dwg.zip" -DestinationPath "C:\Temp\A11_extracted"
Verify that C:\Temp\A11_extracted\A11.dwg exists and that you are not using any file from the __MACOSX sub‑folder.
2. (Optional) Check the DWG version
Open the file in AutoCAD → File → Properties → DWG version.
If the version is newer than 2022, re‑save it as an older version or upgrade Aspose.CAD.
3. Convert the DWG to PDF with Aspose.CAD
using Aspose.CAD;
using Aspose.CAD.ImageOptions;
using Aspose.CAD.LoadOptions;
string dwgPath = @"C:\Temp\A11_extracted\A11.dwg";
// -----------------------------------------------------------------
// Load options – only needed if you must force a specific version.
// -----------------------------------------------------------------
var loadOptions = new CadLoadOptions
{
// Example (uncomment if required):
// LoadVersion = CadVersion.Dwg2022
};
// -----------------------------------------------------------------
// Load the drawing
// -----------------------------------------------------------------
using (Image cadImage = Image.Load(dwgPath, loadOptions))
{
// -----------------------------------------------------------------
// PDF export options
// -----------------------------------------------------------------
var pdfOptions = new PdfOptions
{
// Rasterize the vector drawing at the desired DPI.
// The property name is VectorRasterizationOptions.
VectorRasterizationOptions = new CadRasterizationOptions
{
Dpi = 300, // adjust as needed
PageSize = CadPageSize.A4, // or any other size
// You can also set BackgroundColor, Layout, etc.
}
};
string pdfPath = @"C:\Temp\A11.pdf";
cadImage.Save(pdfPath, pdfOptions);
}
What the code does
- Loads the DWG with optional
CadLoadOptions.
- Creates
PdfOptions and assigns a CadRasterizationOptions object to VectorRasterizationOptions (this controls DPI, page size, background colour, etc.).
- Saves the image as a PDF.
4. If the same exception persists
- The DWG is either corrupted or from a newer version that the current Aspose.CAD build cannot parse.
- Obtain a clean copy of the file or upgrade to the latest Aspose.CAD release (download from the official Aspose site and replace the DLLs).
5. Verify the output
Open C:\Temp\A11.pdf in any PDF viewer to confirm that the conversion succeeded.
Quick checklist
- [ ] Extract the archive and use the real
A11.dwg (ignore __MACOSX files).
- [ ] If the DWG version is newer than 2022, re‑save it in an older version or upgrade Aspose.CAD.
- [ ] Load with
Image.Load(dwgPath, new CadLoadOptions()) (set LoadVersion only when needed).
- [ ] Export using
PdfOptions with VectorRasterizationOptions = new CadRasterizationOptions { … }.
- [ ] If the error remains, obtain a clean DWG copy or use a newer Aspose.CAD version.
Feel free to let me know the exact DWG version (e.g., “DWG 2023”) if you need guidance on which Aspose.CAD build adds support for it.
@PangXiaogang,
您好,
感谢您提供的详细信息,我们已经重现了该问题,并创建了 CADJAVA-12043 来修复它。