XMLWorker报错:'fonts'在当前上下文中不存在,查资料未解决
Hey there! Let's sort out that frustrating error you're hitting: The name fonts does not exist in the current context. This error is telling you that somewhere in your code, you're referencing a variable/object named fonts that hasn't been defined or isn't accessible in your current code scope. Since you're working on font handling with XMLWorker, this almost always ties to missing setup for font providers.
Let's walk through how to fix this step by step:
1. Understand the root issue
When configuring XMLWorker to handle fonts in your HTML-to-PDF conversion, you need to explicitly define a font provider (an instance of IFontProvider) that tells iTextSharp where to find fonts. If your code has a line like CSSResolver.SetFonts(fonts) or similar, fonts needs to be a properly initialized font provider object—right now, it's not, hence the error.
2. Correctly set up the font provider
First, make sure you have all the necessary using directives at the top of your file:
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.tool.xml; using iTextSharp.tool.xml.pipeline.css; using iTextSharp.tool.xml.pipeline.html;
Then, create and configure your font provider. You can either use the default provider or register custom fonts:
// Option 1: Use default fonts plus register custom ones var fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); // Register a custom TTF font (replace with your font path) fontProvider.Register(@"C:\fonts\your-custom-font.ttf"); // Or register system fonts if needed fontProvider.RegisterDirectory(@"C:\Windows\Fonts");
3. Attach the font provider to your CSS resolver
Now, link this font provider to your existing CSSResolver instance:
iTextSharp.tool.xml.pipeline.css.ICSSResolver CSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true); // Add the font provider to the resolver CSSResolver.AddFontProvider(fontProvider); // If you need to set default font properties, you can use this instead: // CSSResolver.SetFonts(fontProvider);
4. Update your full conversion code (replace outdated HTMLWorker)
Quick note: HTMLWorker is deprecated in favor of XMLWorker, so let's adjust your code to use the modern approach to avoid other potential issues:
StringReader sr = new StringReader(body.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); // Initialize PdfWriter PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream("output.pdf", FileMode.Create)); pdfDoc.Open(); // Setup CSS resolver and font provider var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true); var fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); fontProvider.Register(@"path/to/your/font.ttf"); // Add your fonts here cssResolver.AddFontProvider(fontProvider); // Setup HTML pipeline context var cssAppliers = new CssAppliersImpl(fontProvider); var htmlContext = new HtmlPipelineContext(cssAppliers); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); // Build the pipeline and parse the HTML var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer))); var xmlWorker = new XMLWorker(pipeline, true); var xmlParser = new XMLParser(xmlWorker); xmlParser.Parse(sr); pdfDoc.Close();
Key check
Go through your original code and find any line that references fonts—replace that variable name with your properly initialized fontProvider (or whatever name you gave your font provider instance). That's the main fix for the "does not exist in current context" error.
内容的提问来源于stack exchange,提问作者Kenny

