Canon 865 §2. An adult in danger of death can be baptized if, having some knowledge of the principal truths of the faith, the person has manifested in any way at all the intention to receive baptism and promises to observe the commandments of the Christian religion.
And the Catechism states:
160 To be human, "man's response to God by faith must be free, and... therefore** nobody is to be forced to embrace the faith against his will*. the act of faith is of its very nature a free act." "God calls men to serve him in spirit and in truth. Consequently they are bound to him in conscience, but not coerced. . . This fact received its fullest manifestation in Christ Jesus." Indeed, Christ invited people to faith and conversion,* but never coerced them**. "For he bore witness to the truth but refused to use force to impose it on those who spoke against it. His kingdom... grows by the love with which Christ, lifted up on the cross, draws men to himself."
The Church is quite clear that no one should be baptized against their will.
While I am unable to find an official Church pronouncement on whether a forced baptism is invalid as well as illict, theologians have been virtually unanimous in rejecting forced baptisms of adults as valid.