{"record":{"id":"6a49e1c29f2777fd","repo":"QuestPDF/QuestPDF","slug":"the-decoration-before-layer-has-already-been-def","errorCode":null,"errorMessage":"The 'Decoration.Before' layer has already been defined. Please call this method only once.","messagePattern":"The 'Decoration\\.Before' layer has already been defined\\. Please call this method only once\\.","errorType":"exception","errorClass":"DocumentComposeException","httpStatus":null,"severity":"error","filePath":"src/dotnet/library/QuestPDF/Fluent/DecorationExtensions.cs","lineNumber":27,"sourceCode":"    public sealed class DecorationDescriptor\n    {\n        internal Decoration Decoration { get; } = new Decoration();\n\n        internal DecorationDescriptor()\n        {\n            \n        }\n        \n        /// <summary>\n        /// Returns a container for the section positioned before (above) the primary main content.\n        /// </summary>\n        /// <remarks>\n        /// This container is fully visible on each page and does not support paging.\n        /// </remarks>\n        public IContainer Before()\n        {\n            if (Decoration.Before is not (Empty or DebugPointer))\n                throw new DocumentComposeException(\"The 'Decoration.Before' layer has already been defined. Please call this method only once.\");\n\n            var container = new Container();\n            Decoration.Before = container;\n            \n            return container\n                .DebugPointer(DebugPointerType.ElementStructure, \"Before\")\n                .RepeatAsHeader();\n        }\n        \n        /// <summary>\n        /// Provides a handler to the section that appears before (above) the main content.\n        /// </summary>\n        /// <remarks>\n        /// This container is fully visible on each page and does not support paging.\n        /// </remarks>\n        public void Before(Action<IContainer> handler)\n        {\n            handler?.Invoke(Before());","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/QuestPDF/QuestPDF/blob/43ab12559671556e10a6a0a5afe542c919f48a21/src/dotnet/library/QuestPDF/Fluent/DecorationExtensions.cs#L9-L45","documentation":"A Decoration has three named layers — Before, Content, After — and each may be defined only once. Calling .Before() (or the Before(handler) overload) a second time throws DocumentComposeException because the layer is already populated. Only an Empty or DebugPointer placeholder counts as 'undefined'.","triggerScenarios":"Invoking .Before() twice on the same Decoration container, or calling both .Before() and .Before(handler) on it. The guard checks whether Decoration.Before is anything other than Empty/DebugPointer.","commonSituations":"Conditional code paths that both call Before; refactoring that merged two setup blocks; copy-paste of a header definition; framework code plus user code both writing Before.","solutions":["Define Before exactly once per Decoration; branch the content inside that single call.","If you need conditional header content, build it inside the one Before() lambda.","Search for all .Before( calls on the same decoration instance and consolidate them."],"exampleFix":"// before\ndecoration.Before().Text(\"Header\");\n// later\ndecoration.Before().Text(\"Other\");  // throws\n// after\ndecoration.Before(b => b.Text(showOther ? \"Other\" : \"Header\"));","handlingStrategy":"validation","validationCode":"// Structural guard: define Before in exactly one place. If you cannot guarantee that,\n// track it with a flag:\nbool beforeDefined = false;\nif (!beforeDefined) { decoration.Before(b => b.Text(header)); beforeDefined = true; }","typeGuard":null,"tryCatchPattern":"try\n{\n    decoration.Before(b => b.Text(header));\n}\ncatch (DocumentComposeException ex) when (ex.Message.Contains(\"Decoration.Before\"))\n{\n    // Before was already defined elsewhere; consolidate content instead of calling twice.\n    throw;\n}","preventionTips":["Define each Decoration layer (Before/Content/After) exactly once.","Branch conditional content inside the single layer call, not by calling the layer twice.","Audit all calls to .Before(/.Content(/.After( on the same instance."],"tags":["questpdf","decoration","compose","validation","fluent"],"backgroundTag":null,"analyzedSha":"43ab12559671556e10a6a0a5afe542c919f48a21","analyzedAt":"2026-08-13T17:02:41.303Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}