使用iTextSharp 5.4.4时遇‘找不到Common类型或命名空间’错误求解决
解决iTextSharp 5.4.4中"the type or namespace common could not be found"错误
别急,我来帮你分析这个问题——你遇到的「the type or namespace common could not be found」错误,根源其实不在iTextSharp本身,而是代码里的pdfWriter.PageEvent = new Common.ITextEvents();这一行出了问题,下面给你拆解可能的原因和解决办法:
核心问题说明
iTextSharp 5.4.4本身没有自带Common命名空间,你代码里的Common.ITextEvents应该是你自己定义的页面事件类(用来处理页眉、页脚、页码这类逻辑),但当前项目找不到这个命名空间对应的类,所以抛出了错误。
可能的原因及解决方案
自定义的
Common命名空间未正确设置
如果你已经写了ITextEvents类,请检查:- 该类的文件是否已经添加到当前项目中(没有被意外排除);
- 类的命名空间是否确实是
Common(C#是大小写敏感的,注意不要写成common或其他拼写)。
示例正确的ITextEvents类结构:
namespace Common { using iTextSharp.text; using iTextSharp.text.pdf; public class ITextEvents : PdfPageEventHelper { // 重写你需要的页面事件方法,比如添加页码 public override void OnEndPage(PdfWriter writer, Document document) { // 这里写你的页眉/页脚逻辑 BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Font font = new Font(bf, 10, Font.NORMAL); ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase($"Page {writer.PageNumber}"), document.PageSize.GetRight(100), document.PageSize.GetBottom(30), 0); } } }缺少对包含
Common命名空间的程序集引用
如果ITextEvents类是来自你自己写的另一个类库项目,或者第三方dll,那需要在当前项目中添加对该程序集的引用:
右键你的项目 → 选择「添加」→「引用」,找到对应的类库项目或dll文件并添加。拼写错误或命名空间混淆
检查代码里的Common拼写是否和实际命名空间一致,比如有没有少写字母、大小写错误,或者误把其他命名空间写成了Common。
内容的提问来源于stack exchange,提问作者Milan Srdić
相关产品推荐
相关产品推荐

