好奇怪的问题 “ 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?)”

随便聊聊 · 1005 次浏览
KBC 创建于 2024-07-11 10:31

这是代码:

//.cs  文件类型,便于外部编辑时使用
// 引用必要的命名空间
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Threading.Tasks;

public static void Exec(Quicker.Public.IStepContext context)
{
    string folderPath = (string)context.GetVarValue("每个文件夹路径"); // 获取图片文件夹路径
    string destination = (string)context.GetVarValue("转换后的PDF路径");

    if (!string.IsNullOrEmpty(folderPath) && Directory.Exists(folderPath))
    {
        var imageExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; // 图像文件扩展名列表
        var imageFiles = Directory.GetFiles(folderPath)
                                  .Where(f => imageExtensions.Contains(Path.GetExtension(f).ToLower()))
                                  .OrderBy(f => f)
                                  .ToList();

        if (imageFiles.Count > 0)
        {
            PdfDocument doc = new PdfDocument();

            // 使用并行处理以加快处理速度
            Parallel.ForEach(imageFiles, imagePath =>
            {
                using (Bitmap bitmap = new Bitmap(imagePath))
                {
                    int width = bitmap.Width;
                    int height = bitmap.Height;
                    float dpiX = bitmap.HorizontalResolution;
                    float dpiY = bitmap.VerticalResolution;

                    PdfPage pdfPage = doc.AddPage();
                    pdfPage.Width = XUnit.FromInch(width / dpiX);
                    pdfPage.Height = XUnit.FromInch(height / dpiY);

                    using (XGraphics xgr = XGraphics.FromPdfPage(pdfPage))
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            bitmap.Save(stream, ImageFormat.Png);
                            stream.Seek(0, SeekOrigin.Begin);

                            XImage img = XImage.FromStream(stream);
                            xgr.DrawImage(img, 0, 0, pdfPage.Width.Point, pdfPage.Height.Point);
                        }
                    }
                }
            });

            // 保存PDF文件
            doc.Save(destination);
            doc.Close();
        }
        else
        {
            context.SetVarValue("error", "文件夹中没有找到有效的图片文件");
        }
    }
    else
    {
        context.SetVarValue("error", "未提供有效的图片文件夹路径或文件夹不存在");
    }
}

这是报错:

异常:c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(9,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?) c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(10,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?)
在 csscript.CSExecutor.ProcessCompilingResult(CompilerResults results, CompilerParameters compilerParams, ScriptParser parser, String scriptFileName, String assemblyFileName, String[] additionalDependencies) 在 csscript.CSExecutor.Compile(String scriptFileName) 在 CSScriptLibrary.CSScript.LoadWithConfig(String scriptFile, String assemblyFile, Boolean debugBuild, Settings scriptSettings, String compilerOptions, String[] refAssemblies) 在 CSScriptLibrary.CSScript.LoadCode(String scriptText, String tempFileExtension, String assemblyFile, Boolean debugBuild, String[] refAssemblies) 在 CSScriptLibrary.CodeDomEvaluator.CompileCode(String scriptText) 在 V9XrJESM2EmK5n8SuUQ.A46DZQSrEyKNqJiQvbk.mxoWECaNP4i(String , String , Boolean , ActionExecuteContext ) 在 Quicker.Domain.Actions.X.BuiltinRunners.Other.RunCsScriptStep.rmBWhyCXEGE(ActionStep , ActionExecuteContext , XAction , String , ThreadType ) 在 Quicker.Domain.Actions.X.BuiltinRunners.Other.RunCsScriptStep.<>c__DisplayClass62_0.VNxhuugWSGW() 在 Quicker.Domain.Actions.X.XActionHelper.ExecuteCommonAction(ActionExecuteContext context, ActionStep step, XAction action, Func`1 actionFunc, Action successAction, Action failAction, StepInParamDef stopIfErrorParam, StepOutParamDef isSuccessOutputParam)
[out]是否成功=>isSuccessFalse
[out]错误消息=>errMessagec:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(9,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?)\r\n c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(10,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?)\r\n
步骤(sys:csscript)执行失败,原因:c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(9,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?) c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(10,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?)
停止动作:c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(9,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?) c:\Users\kouba\AppData\Local\Temp\CSSCRIPT\dynamic\10876.251000ed-cb13-4cc6-bec6-3d3290ad5450.tmp(10,7): error CS0246: 未能找到类型或命名空间名称“PdfSharp”(是否缺少 using 指令或程序集引用?) 
 
问题:有时候执行的时候就报这个错误,但是呢当有一次成功执行了,后面就没问题了,好奇怪啊,看着引用的也没问题,大小写也对。不知道哪里的问题

回复内容
CL 2024-07-11 11:28
#1

这个提示一般是代码问题。 但是有时候可以就比较奇怪了。 

建议直接发动作网址。

KBC 回复 CL 2024-07-11 11:58 :

https://getquicker.net/Sharedaction?code=f1564a50-c2f4-4c19-2fa2-08dc9fa8ef9a&fromMyShare=true

CL 回复 KBC 2024-07-11 15:30 :

步骤里没有引用dll文件。

回复主贴